forked from fengmap/fm-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (64 loc) · 1.85 KB
/
app.js
File metadata and controls
73 lines (64 loc) · 1.85 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
71
72
73
// app.js
var http = require('http');
var fs = require('fs'); //引入文件读取模块
var url = require('url');
var path = require('path');
var zlib = require("zlib");
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": "text/plain",
"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",
"fmi": "image/png"
};
// var documentRoot = 'C:/Users/easyfrog/Desktop/threetest';
var documentRoot = process.cwd();
http.createServer(function(req,response){
var pathname = url.parse(req.url).pathname;
//客户端输入的url,例如如果输入localhost:8888/index.html
//那么这里的url == /index.html
var realPath = path.join(documentRoot, path.normalize(pathname.replace(/\.\./g, "")));
fs.exists(realPath, function (exists) {
if (!exists) {
response.writeHead(404, {
'Content-Type': 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
fs.readFile(realPath, "binary", function (err, file) {
if (err) {
response.writeHead(500, {
'Content-Type': 'text/plain'
});
response.end(err);
} else {
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
var contentType = mime[ext] || "text/plain";
response.writeHead(200, {
'content-type' : contentType
});
response.write(file, "binary");
response.end();
}
});
}
});
}).listen(8084, '0.0.0.0', function() {
console.log('Https server listening on port 8084');
});