-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-static.js
More file actions
34 lines (29 loc) · 1002 Bytes
/
http-static.js
File metadata and controls
34 lines (29 loc) · 1002 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
30
31
32
33
34
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
const server = http.createServer((req, res) => {
let filePath = path.resolve(__dirname, path.join('www', url.fileURLToPath(`file:///${req.url}`)));
console.log(`Request: ${filePath}`);
if(fs.existsSync(filePath)) {
const stats = fs.statSync(filePath);
if(stats.isDirectory()) {
filePath = path.join(filePath, 'index.html');
}
if(fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath);
const {ext} = path.parse(filePath);
res.writeHead(200, {'Content-Type': mime.getType(ext)});
return res.end(content);
}
}
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Not Found</h1>');
});
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());
});