-
Notifications
You must be signed in to change notification settings - Fork 84
/
server.js
44 lines (34 loc) · 1.12 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
40
41
42
43
const http = require('http');
const fs = require("fs");
const path = require("path");
const port = process.env.PORT || 3000;
function status404(response) {
response.writeHead(404, {'Content-Type': 'text/html'});
response.write('<h1>404 Not Found</h1>');
response.end();
}
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.js': 'application/javascript'
};
function getFilename(req) {
if (req.url === '/') return 'index.html';
return req.url;
}
http.createServer((request, response) => {
if (request.method === 'GET') {
let filename = getFilename(request);
let filepath = path.resolve('./public/' + filename);
let mimeType = mimeTypes[path.extname(filepath)];
if (!mimeType) return status404(response);
fs.exists(filepath, (exists) => {
if (!exists) return status404(response);
response.writeHead(200, {'Content-Type': mimeType});
fs.createReadStream(filepath).pipe(response);
});
}
}).listen(port);
console.log("Server running at port " + port);