-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.js
76 lines (73 loc) · 1.9 KB
/
1.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
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
71
72
73
74
75
76
console.time("[WebSvr][Start]");
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
function funGetContentType(filePath){
var contentType = "";
var ext = path.extname(filePath);
console.log("extname: " + ext);
switch(ext){
case ".html":
contentType = "text/html";
break;
case ".js":
contentType = "text/javascript";
break;
case ".css":
contentType = "text/css";
break;
case ".gif":
contentType = "image/gif";
break;
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".ico":
contentType = "image/icon";
break;
default:
contentType = "application/octet-stream";
}
return contentType;
}
function funWebSvr(req,res){
var requrl = req.url;
var pathName = url.parse(requrl).pathname;
console.log("parse pathName :" + pathName);
if(path.extname(pathName) == ""){
pathName = "/";
}
if(pathName.charAt(pathName.length - 1) == "/"){
pathName += "index.html";
}
console.log("pathName:" +pathName);
var filePath = pathName.slice(1);
console.log("filePath:" + filePath);
fs.exists(filePath,function(exists){
if(exists){
res.writeHead(200,{"Content-Type":funGetContentType(filePath)});
var stream = fs.createReadStream(filePath,{flags:"r",encoding:null});
stream.on("error",function(){
res.writeHead(404);
res.end("<h1>404 Not Found</h1>");
});
stream.pipe(res);
}else {
console.log("The file does not exist!");
res.writeHead(404,{"Content-Type":"text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}
var webSvr = http.createServer(funWebSvr);
webSvr.on("error",function(error){
console.log(error);
});
webSvr.listen(8000,function(){
console.log("[WebSvr][Start] running at localhost:8000");
console.timeEnd("[WebSvr][Start]");
});