-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (26 loc) · 859 Bytes
/
Copy pathindex.js
File metadata and controls
31 lines (26 loc) · 859 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
const http = require('http');
const url = require('url');
const fs = require('fs');
const page404 =
fs.readFileSync('404.html', (err, data) => {
if (err) throw err;
return data;
});
http.createServer((req, res) => {
const q = url.parse(req.url, true).pathname;
let loc = '.' + q;
if (loc === './') loc = 'index.html';
fs.readFile(loc, (err, data) => {
if (err) {
res.writeHead(404, { 'content-type': 'text/html' });
res.end(page404);
}
else {
fs.readFile(loc, (err, data) => {
if (err) throw err;
res.writeHead(200, { 'content-type': 'text/html' });
res.end(data);
});
}
})
}).listen(5501, () => console.log(`Server running at http://localhost:5500/`))