-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-mime2.js
More file actions
29 lines (23 loc) · 783 Bytes
/
Copy pathhttp-mime2.js
File metadata and controls
29 lines (23 loc) · 783 Bytes
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 mime = require('mime');
const server = http.createServer((req, res) => {
const srvUrl = url.parse(`http://${req.url}`);
let path = srvUrl.path;
if(path === '/') path = '/index.html';
const resPath = `resource${path}`;
if(!fs.existsSync(resPath)) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end('<h1>404 Not Found</h1>');
}
const resStream = fs.createReadStream(resPath);
res.writeHead(200, {'Content-Type': mime.getType(resPath)});
resStream.pipe(res);
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(10080, () => {
console.log('opened server on', server.address());
});