-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (46 loc) · 1.43 KB
/
Copy pathserver.js
File metadata and controls
62 lines (46 loc) · 1.43 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
"use strict";
var fs = require("fs");
var http = require("http");
var url = require("url");
var path = require("path");
var rootPath = "./dist";
//mime 文件,用来做mine类型匹配的。网上能下载
var mime = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml"
};
var server = http.createServer(function(req,res){
var reqPath = url.parse(req.url,true).pathname;
if(reqPath == "/favicon.ico") return ;
var extname = path.extname(reqPath);
var mineType = mime[extname] || "text/plain";
if(reqPath == "/") reqPath = "/index.html";
var targetPath = rootPath + reqPath;
fs.readFile(targetPath,function(err,data){
if(err) {
res.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
res.end("请求路径不合法");
} else {
res.writeHead(200,{"Context-Type":""+mineType+";charset=UTF-8"});
res.end(data);
}
});
});
server.listen(9127,"0.0.0.0");
console.log("server run at: http://127.0.0.1:9127")