質問編集履歴
1
コードの追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,11 +4,111 @@
|
|
4
4
|
(※js内でWebサーバーをたてています)
|
5
5
|
|
6
6
|
### 発生している問題・エラーメッセージ
|
7
|
-
|
7
|
+
普通に記述しただけでは「ReferenceError: require is not defined」というエラーが出るため、エラーの出ない方法を探しています。
|
8
8
|
|
9
9
|
Browserifyを使用したところrequireのエラーは消えましたが、
|
10
|
-
|
10
|
+
「TypeError: http.createServer is not a function」というエラーが出ます。
|
11
11
|
|
12
|
+
### 該当のソースコード
|
13
|
+
長い為、一部抜粋して掲載いたします。
|
14
|
+
|
15
|
+
```html
|
16
|
+
<!DOCTYPE html>
|
17
|
+
<html lang="ja">
|
18
|
+
|
19
|
+
<head>
|
20
|
+
<meta http-equiv="content-type" charset="utf-8">
|
21
|
+
<title>testページ</title>
|
22
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
|
23
|
+
<script type="text/javascript" src="./bundle.js"></script>
|
24
|
+
</head>
|
25
|
+
<body>
|
26
|
+
<form id="form1" name="form1">
|
27
|
+
<input type="text" name="q4_2_other" size="50">
|
28
|
+
<input type="submit" name="btn" id="btn" value="送 信" onclick="createPdf()">
|
29
|
+
</form>
|
30
|
+
<script>
|
31
|
+
$('#form1').submit(function(event) {
|
32
|
+
var formData = $('form1').serialize();
|
33
|
+
// ここでsubmitをキャンセルします。
|
34
|
+
event.preventDefault();
|
35
|
+
// Ajax処理
|
36
|
+
$.ajax({
|
37
|
+
url: 'http://localhost:8080/',
|
38
|
+
type:'POST',
|
39
|
+
dataType: 'json',
|
40
|
+
data: formData,
|
41
|
+
timeout:10000
|
42
|
+
});
|
43
|
+
});
|
44
|
+
</script>
|
45
|
+
</body>
|
46
|
+
</html>
|
47
|
+
```
|
48
|
+
```javaScript
|
49
|
+
var PDFDocument = require('pdfkit');
|
50
|
+
var fs = require('fs');
|
51
|
+
var http = require("http"),
|
52
|
+
url = require("url"),
|
53
|
+
path = require("path"),
|
54
|
+
port = process.argv[2] || 8080;
|
55
|
+
|
56
|
+
http.createServer(function(request, response) {
|
57
|
+
var Response = {
|
58
|
+
"200":function(file, filename){
|
59
|
+
var extname = path.extname(filename);
|
60
|
+
var header = {
|
61
|
+
"Access-Control-Allow-Origin":"*",
|
62
|
+
"Pragma": "no-cache",
|
63
|
+
"Cache-Control" : "no-cache"
|
64
|
+
}
|
65
|
+
|
66
|
+
response.writeHead(200, header);
|
67
|
+
response.write(file, "binary");
|
68
|
+
response.end();
|
69
|
+
},
|
70
|
+
"404":function(){
|
71
|
+
response.writeHead(404, {"Content-Type": "text/plain"});
|
72
|
+
response.write("404 Not Found\n");
|
73
|
+
response.end();
|
74
|
+
|
75
|
+
},
|
76
|
+
"500":function(err){
|
77
|
+
response.writeHead(500, {"Content-Type": "text/plain"});
|
78
|
+
response.write(err + "\n");
|
79
|
+
response.end();
|
80
|
+
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
|
85
|
+
var uri = url.parse(request.url).pathname
|
86
|
+
, filename = path.join(process.cwd(), uri);
|
87
|
+
|
88
|
+
fs.exists(filename, function(exists){
|
89
|
+
console.log(filename+" "+exists);
|
90
|
+
if (!exists) { Response["404"](); return ; }
|
91
|
+
if (fs.statSync(filename).isDirectory()) { filename += './index.html'; }
|
92
|
+
|
93
|
+
fs.readFile(filename, "binary", function(err, file){
|
94
|
+
if (err) { Response["500"](err); return ; }
|
95
|
+
Response["200"](file, filename);
|
96
|
+
});
|
97
|
+
|
98
|
+
});
|
99
|
+
|
100
|
+
|
101
|
+
}).listen(parseInt(port, 10));
|
102
|
+
|
103
|
+
console.log("Server running at http://localhost:" + port );
|
104
|
+
function createPdf(){
|
105
|
+
|
106
|
+
// 以降、pdf作成処理
|
107
|
+
|
108
|
+
}
|
109
|
+
|
110
|
+
```
|
111
|
+
|
12
112
|
### 補足情報(FW/ツールのバージョンなど)
|
13
113
|
browserify:ver.16.2.2
|
14
114
|
Node.js:v7.2.1
|