-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·49 lines (40 loc) · 1.35 KB
/
app.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
const http = require('http');
const Path=require("path");
const fs=require("fs");
var server = http.createServer(function (req, res){
let fileName=Path.resolve(__dirname,"."+req.url).split("?")[0];
if (req.url === '/'){
fileName = Path.resolve(__dirname, 'index.html');
}
console.log(fileName);
const extName=Path.extname(fileName).substr(1);
if (fs.existsSync(fileName)) {
var mineTypeMap={
html:'text/html;charset=utf-8',
htm:'text/html;charset=utf-8',
xml:"text/xml;charset=utf-8",
png:"image/png",
jpg:"image/jpeg",
jpeg:"image/jpeg",
gif:"image/gif",
css:"text/css;charset=utf-8",
txt:"text/plain;charset=utf-8",
mp3:"audio/mpeg",
mp4:"video/mp4",
ico:"image/x-icon",
tif:"image/tiff",
svg:"image/svg+xml",
zip:"application/zip",
ttf:"font/ttf",
woff:"font/woff",
woff2:"font/woff2",
js: "text/javascript;charset=utf-8"
}
if (mineTypeMap[extName]) {
res.setHeader('Content-Type', mineTypeMap[extName]);
}
var stream=fs.createReadStream(fileName);
stream.pipe(res);
}
}).listen(8888);
console.log('服务器监听端口:'+ server.address().port);