-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstaticFile.js
More file actions
77 lines (72 loc) · 1.87 KB
/
staticFile.js
File metadata and controls
77 lines (72 loc) · 1.87 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
/**
* Created by addison on 14-6-23.
*
* 请求静态文件处理函数
* 类型:
*
* html
* js
* css
* image - [bmp,png,jpg,jpeg,gif]
*
*
*/
var fs = require('fs');
function staticFile(req, res) {
var url = req.url;
var enter = {
base: '/!b',
display: '/!d',
front: '/!f',
login: '/!l'
}
var mime = {
js: 'application/x-javascript',
css: 'text/css',
html: 'text/html',
png: 'image/png',
gif: 'image/gif',
jpeg: 'image/jpeg',
bmp: 'image/bmp',
jpg: 'image/jpg'
}
var cache = [];
var filePath = url;
var ct = 'text/html';
if (url == enter.base) {
filePath = 'client/base/base.html';
} else if (url == enter.display) {
filePath = 'client/display/display.html';
} else if (url == enter.front) {
filePath = 'client/front/front.html';
} else if (url == '/' || url == enter.login) {
filePath = 'client/login.html';
} else { //其他正常路径的js css html
var fileType = filePath.split('.')[1];
if (url == '/socket.io/socket.io.js') {
//排除 socket.io.client
filePath = '';
res.end('');
} else {//js css html image
filePath = filePath.substr(1, filePath.length);
ct = mime[fileType];
}
}
if (filePath != '') {
ct = ct + ';charset=utf-8';
//console.log(filePath);
// console.log(filePath);
fs.exists(filePath, function (a) {
if (a) {
fs.readFile(filePath, function (err, data) {
res.writeHead(200, {'Content-Type': ct});
res.write(data, 'binary');
res.end();
});
} else {
res.end("not resource");
}
})
}
}
exports.sf = staticFile;