app01.js
1.33 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
41
42
43
44
"use strict"
var http = require('http');
var path = require('path');
var url = require('url');
var fs = require('fs');
var DOCUMENT_ROOT = "../Responsive_Web_Design/"; // 상대경로로 루트를 설정함.
// 서버를 생성함
var server = http.createServer(function(req, res) {
var reqPath = url.parse(req.url).pathname;
if (reqPath == "/") {
reqPath = "Responsive_Design_Quiz_2012104136.html";
}
var fullPath = path.join(process.cwd(), DOCUMENT_ROOT, reqPath);
fs.readFile(fullPath, "binary", function(err, file) {
if (err) {
// 파일 없으면 에러
if (err.code == "ENOENT") {
console.log("SEND 404 for " + req.url);
res.writeHeader(404, { "Content-Type": "text/html" });
res.write("<h1>Not found</h1>");
res.end();
} else {
console.error("Error", err);
res.writeHeader(500, { "Content-Type": "text/plain" });
res.write(err + "\n");
res.end();
}
} else {
console.log("SEND 200 for " + req.url);
res.writeHeader(200);
res.write(file, "binary");
res.end();
}
})
});
server.listen(3000, function() {
console.log("Server listening on http://localhost:3000");
});