-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (36 loc) · 1.23 KB
/
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
33
34
35
36
37
38
39
const http = require('http');
const fs = require('fs');
const url = require('url');
const mime = require('mime');
http.createServer((req, res) => {
let requestedFile;
switch (req.url) {
case '/':
requestedFile = __dirname + '/index.html';
const readStream = fs.createReadStream(requestedFile);
res.setHeader("Content-type", mime.getType(requestedFile))
readStream.on('close', () => {
res.end();
});
readStream.pipe(res);
return;
default:
requestedFile = __dirname + req.url;
fs.stat(requestedFile, (err, stats) => {
if (err) {
res.end();
return;
} else {
if (stats.isFile()) {
const readStream = fs.createReadStream(requestedFile);
res.setHeader("Content-type", mime.getType(requestedFile))
readStream.on('close', () => {
res.end();
});
readStream.pipe(res)
}
}
});
return;
}
}).listen(4040, 'localhost')