-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp-mime.js
44 lines (36 loc) · 1.11 KB
/
http-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
const http = require('http');
const url = require('url');
const fs = require('fs');
function getMimeType(res) {
const EXT_MIME_TYPES = {
default: 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'text/json',
'.jpeg': 'image/jpeg',
'.jpg': 'image/jpg',
'.png': 'image/png',
};
const path = require('path');
const mime_type = EXT_MIME_TYPES[path.extname(res)] || EXT_MIME_TYPES.default;
return mime_type;
}
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': getMimeType(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());
});