-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_server.js
More file actions
70 lines (60 loc) · 1.52 KB
/
node_server.js
File metadata and controls
70 lines (60 loc) · 1.52 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//
// SAMPLE NODE SERVER
// 2013.07.17.
// kang hwan ki
//
var http = require("http");
var url = require("url");
var path = require("path");
var fs = require("fs");
var SERVER_INFO = {
IP : "127.0.0.1",
PORT : 9000
};
var CONTENT_TYPE = {
HTML : "text/html",
CSS : "text/css",
JS : "text/javascript",
XML : "text/xml"
};
var action = function(req,res) {
var uri = url.parse(req.url).pathname;
var filename = path.join(process.cwd(),uri);
console.log("[INFO] : uri : "+uri);
console.log(" : file : "+filename);
console.log("");
path.exists(filename, function(exists){
if(!exists){
res.writeHead(404, {"Content-Type":"text/plain"});
res.write("404 Not Found\n");
res.end();
return;
}
if(fs.statSync(filename).isDirectory()){
filename += "/index.html";
}
fs.readFile(filename, "binary", function(err, file){
if(err){
res.writeHead(500, {"Content-Type":"text/plain"});
res.write(err + "\n");
res.end();
return;
}
var key = path.extname(filename).replace(".","").toUpperCase();
var contentType = CONTENT_TYPE[key] || "text/plain";
var headers = {"Content-Type":contentType};
res.writeHead(200,headers);
res.write(file,"binary");
res.end();
return;
});
});
};
http.createServer(action).listen(SERVER_INFO.PORT, SERVER_INFO.IP);
console.log("IP : "+SERVER_INFO.IP);
console.log("PORT : "+SERVER_INFO.PORT);
console.log("Start web server...");
console.log("Stop web server command is [Ctrl + C]");
console.log(".");
console.log(".");
console.log(".");