forked from imwtr/rem-vw-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
32 lines (25 loc) · 878 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
30
31
32
let http = require('http'),
fs = require('fs'),
path = require('path'),
url = require('url');
http.createServer((req, res) => {
let {pathname} = url.parse(req.url),
raw;
console.log('Request: ', req.url);
try {
raw = fs.createReadStream(path.resolve(__dirname, pathname.replace(/^\//, '')));
raw.on('error', (err) => {
console.log(err);
if (err.code === 'ENOENT') {
res.writeHeader(404, {'content-type': 'text/html;charset="utf-8"'});
res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
res.end();
}
});
res.writeHead(200, {});
raw.pipe(res);
} catch (e) {
console.log(e);
}
}).listen(4321);
console.log('Server listening ', 'http://localhost:4321/');