-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
37 lines (32 loc) · 1.1 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
const http = require('http');
const fs = require('fs');
const url = require('url');
const contentTypes = new Map();
contentTypes.set('html', 'text/html');
contentTypes.set('js', 'text/javascript');
contentTypes.set('css', 'text/css');
contentTypes.set('json', 'application/json');
http.createServer(function (req, res) {
const reqUrl = url.parse(req.url);
const ext = reqUrl.pathname.split('.')[1];
const fileName = reqUrl.pathname.substr(1);
const cType = contentTypes.get(ext);
fs.readFile('public/' + fileName, function(err, data) {
if(err) {
if(err.code == 'ENOENT'){
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('Resource no found');
}
else {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write('Server Error');
}
} else {
res.writeHead(200, {'Content-Type': cType});
res.write(data);
}
res.end();
});
}).listen(8080, function () {
console.log('Client is available at http://localhost:8080');
});