-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfileRoute.js
More file actions
77 lines (74 loc) · 2.19 KB
/
fileRoute.js
File metadata and controls
77 lines (74 loc) · 2.19 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
74
75
76
77
var url = require('url');
var path = require('path');
var querystring = require('querystring');
var fs = require('fs');
module.exports = {
fileType: {
"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"
},
routePathGet: {},
routePathPost: {},
get: function(pathname, fn) {
this.routePathGet[pathname] = fn;
},
post: function(pathname, fn) {
this.routePathPost[pathname] = fn;
},
rootPath: "",
setRootPath: function(rp) {
this.rootPath = rp;
},
sendFile: function(response, pathname) {
var ext = path.extname(pathname).slice(1);
var ft = this.fileType[ext] || "text/plain";
response.setHeader("Content-type", [ft, "charset=utf-8"]);
pathname = this.rootPath + pathname;
pathname = path.normalize(pathname);
fs.readFile(pathname, function(err, data) {
if (err) {
response.statusCode = 404;
response.end('404文件不存在');
} else {
response.statusCode = 200;
response.end(data, "binary");
}
});
},
init: function(request, response) {
var fn = undefined;
var pathname = request.url;
//去除URL中的参数影响
[...pathname].forEach((e,i)=>{
if(e === '?'){
pathname = pathname.slice(0,i)
}
})
if (request.method.toLowerCase() === 'get') {
fn = this.routePathGet[pathname];
} else {
fn = this.routePathPost[pathname];
}
if (fn) {
fn(request, response);
} else {
this.sendFile(response, pathname);
}
}
};