質問編集履歴

1

コードの追記

2018/05/30 02:26

投稿

atmn3356
atmn3356

スコア157

test CHANGED
File without changes
test CHANGED
@@ -10,13 +10,213 @@
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
12
 
13
- その際、普通に記述しただけでは```ReferenceError: require is not defined```とエラーが出るため、エラーの出ない方法を探しています。
13
+ 普通に記述しただけではReferenceError: require is not definedいうエラーが出るため、エラーの出ない方法を探しています。
14
14
 
15
15
 
16
16
 
17
17
  Browserifyを使用したところrequireのエラーは消えましたが、
18
18
 
19
- ```TypeError: http.createServer is not a function```というエラーが出ます。
19
+ TypeError: http.createServer is not a functionというエラーが出ます。
20
+
21
+
22
+
23
+ ### 該当のソースコード
24
+
25
+ 長い為、一部抜粋して掲載いたします。
26
+
27
+
28
+
29
+ ```html
30
+
31
+ <!DOCTYPE html>
32
+
33
+ <html lang="ja">
34
+
35
+
36
+
37
+ <head>
38
+
39
+ <meta http-equiv="content-type" charset="utf-8">
40
+
41
+ <title>testページ</title>
42
+
43
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
44
+
45
+ <script type="text/javascript" src="./bundle.js"></script>
46
+
47
+ </head>
48
+
49
+ <body>
50
+
51
+ <form id="form1" name="form1">
52
+
53
+   <input type="text" name="q4_2_other" size="50">
54
+
55
+   <input type="submit" name="btn" id="btn" value="送 信" onclick="createPdf()">
56
+
57
+ </form>
58
+
59
+ <script>
60
+
61
+ $('#form1').submit(function(event) {
62
+
63
+ var formData = $('form1').serialize();
64
+
65
+ // ここでsubmitをキャンセルします。
66
+
67
+ event.preventDefault();
68
+
69
+ // Ajax処理
70
+
71
+ $.ajax({
72
+
73
+ url: 'http://localhost:8080/',
74
+
75
+ type:'POST',
76
+
77
+ dataType: 'json',
78
+
79
+ data: formData,
80
+
81
+ timeout:10000
82
+
83
+ });
84
+
85
+ });
86
+
87
+ </script>
88
+
89
+ </body>
90
+
91
+ </html>
92
+
93
+ ```
94
+
95
+ ```javaScript
96
+
97
+ var PDFDocument = require('pdfkit');
98
+
99
+ var fs = require('fs');
100
+
101
+ var http = require("http"),
102
+
103
+ url = require("url"),
104
+
105
+ path = require("path"),
106
+
107
+ port = process.argv[2] || 8080;
108
+
109
+
110
+
111
+ http.createServer(function(request, response) {
112
+
113
+ var Response = {
114
+
115
+ "200":function(file, filename){
116
+
117
+ var extname = path.extname(filename);
118
+
119
+ var header = {
120
+
121
+ "Access-Control-Allow-Origin":"*",
122
+
123
+ "Pragma": "no-cache",
124
+
125
+ "Cache-Control" : "no-cache"
126
+
127
+ }
128
+
129
+
130
+
131
+ response.writeHead(200, header);
132
+
133
+ response.write(file, "binary");
134
+
135
+ response.end();
136
+
137
+ },
138
+
139
+ "404":function(){
140
+
141
+ response.writeHead(404, {"Content-Type": "text/plain"});
142
+
143
+ response.write("404 Not Found\n");
144
+
145
+ response.end();
146
+
147
+
148
+
149
+ },
150
+
151
+ "500":function(err){
152
+
153
+ response.writeHead(500, {"Content-Type": "text/plain"});
154
+
155
+ response.write(err + "\n");
156
+
157
+ response.end();
158
+
159
+
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+
168
+
169
+ var uri = url.parse(request.url).pathname
170
+
171
+ , filename = path.join(process.cwd(), uri);
172
+
173
+
174
+
175
+ fs.exists(filename, function(exists){
176
+
177
+ console.log(filename+" "+exists);
178
+
179
+ if (!exists) { Response["404"](); return ; }
180
+
181
+ if (fs.statSync(filename).isDirectory()) { filename += './index.html'; }
182
+
183
+
184
+
185
+ fs.readFile(filename, "binary", function(err, file){
186
+
187
+ if (err) { Response["500"](err); return ; }
188
+
189
+ Response["200"](file, filename);
190
+
191
+ });
192
+
193
+
194
+
195
+ });
196
+
197
+
198
+
199
+
200
+
201
+ }).listen(parseInt(port, 10));
202
+
203
+
204
+
205
+ console.log("Server running at http://localhost:" + port );
206
+
207
+ function createPdf(){
208
+
209
+
210
+
211
+ // 以降、pdf作成処理
212
+
213
+
214
+
215
+ }
216
+
217
+
218
+
219
+ ```
20
220
 
21
221
 
22
222