-
Notifications
You must be signed in to change notification settings - Fork 0
/
mime.js
71 lines (61 loc) · 1.85 KB
/
mime.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
//返回MIME的静态资源文件给客户端
var url=require('url');
var path=require('path');
var fs = require( 'fs' );
//添加MIME类型
var MIME_TYPE = {
"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"
};
function serverStatic(request, response){
var filePath;
if( request.url === "/" ){
filePath = __dirname + "/index.html";
} else{
filePath = __dirname + "/" + url.parse(request.url).pathname;
}
fs.exists(filePath, function(err){
if(!err){
send404( response );
}else{
var ext = path.extname( filePath );
ext = ext ? ext.slice(1) : 'unknown';
var contentType = MIME_TYPE[ext] || "text/plain";
fs.readFile(filePath, function(err, data){
if(err){
send500( response );
}else{
send200( response, contentType, data );
}
});//fs.readfile
}
})//path.exists
}
function send404(res){
res.end('<h1 style="text-align:center;">404<p>file not found</p></h1>' );
}
function send500( resonse ) {
resonse.writeHead( 500 );
resonse.end( '<h1 style="text-align:center;">500<p>务器内部错误!</p></h1>' );
}
function send200( response, contentType, data ) {
response.writeHead( 200, {'content-type':contentType} );
response.end( data );
}
module.exports.serverStatic = serverStatic;