-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
29 lines (28 loc) · 889 Bytes
/
server.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
/**
* 静态文件服务器
*/
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
http.createServer(function (req, res) {
var pathname = url.parse(req.url).pathname;
if (pathname === '/') {
pathname = '/index.html';
}
console.log('req:', req.headers)
if (pathname.indexOf('/api/')===0) {
// 在这里返回json格式的数据
} else {
fs.readFile(path.join(__dirname, pathname), function (err, file) {
if (err) {
res.writeHead(404, {"Content-Type": "text/html;charset:utf-8"});
res.end('404,Not found!');
return;
}
res.writeHead(200, {"Content-Type": "text/html;charset:utf-8"});
res.end(file);
});
console.log('Static server is running! ROOT:', __dirname);
}
}).listen(80);