-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode10.js
More file actions
40 lines (31 loc) · 1.05 KB
/
node10.js
File metadata and controls
40 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var fs = require('fs');
var http = require('http');
var port = process.argv[2];
var path = process.argv[3];
var server = http.createServer(function (req,res) {
req.setEncoding("utf8");
console.log("ok");
var data = "";
var file = fs.createReadStream(path);
file.on("data",function(chunk){
res.write(chunk);
});
file.on("end",function(){
res.end("request end");
});
// req = HTTPのプロパティ
// res = クライアントにヘッダーやボディを返すためのオブジェクト
});
server.listen(port)
/*
res.setEncoding("utf8");出来ないわけ
res.write(data);でdataが読めない
->非同期処理が原因
dataを読む前に
res.end("request end");が呼ばれた.
streamを活かすために,res.write(chunk);を
file.on("data",のなかで呼び出し,少しづつデータを渡す仕様にした.
req.on('end'が読まれなかったのは↓を書いて解決した
req.on("data",function(){});
ブラウザから読む(node09では"net"を使っているためtelnet)
*/