-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (38 loc) · 1.36 KB
/
server.js
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
var http = require("http");
var fs = require("fs");
var url = require("url");
var path = require("path");
var getContentType = function (uri) {
if (uri.indexOf(".html") >= 0) {
return "text/html";
} else if (uri.indexOf(".css") >= 0) {
return "text/css";
} else {
return "text/plain";
}
}
http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname;
var filepath = path.join(process.cwd(), uri);
// check whether the file is exist and get the result from callback
path.exists(filepath, function (exists) {
if (!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
} else {
// read the file content and get the result from callback
fs.readFile(filepath, "binary", function (error, data) {
if (error) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(error + "\n");
} else {
response.writeHead(200, {"Content-Type": getContentType(uri)});
response.write(data, "binary");
}
response.end();
});
}
});
}).listen(8877, "127.0.0.1");
console.log("Listining port 8877");