forked from JeffreyArt1/rgbeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (27 loc) · 874 Bytes
/
index.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
const http = require('http');
const path = require('path');
const fs = require('fs');
const PORT = 3000;
const OK = 200;
const NOT_FOUND = 404;
const requestListener = function (req, res) {
const filepath = './src' + (req.url == '/' ? '/index.html' : req.url);
let fileExtension = path.extname(filepath);
const mime = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.ico': 'image/ico',
};
let content = mime[fileExtension];
console.log(filepath);
console.log(content);
fs.readFile(filepath, (err, data) => {
if (err) console.log(err.code == 'ENOENT' ? NOT_FOUND : 500);
res.writeHead(OK, { 'Content-Type': content });
res.end(data, 'utf-8');
});
};
const server = http.createServer(requestListener);
server.listen(PORT, () => console.log(`Runnin on port ${PORT}`));