-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (35 loc) · 1.15 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const port = 3000;
const mimeTypes = {
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.gif': 'image/gif'
};
const server = http.createServer((req, res) => {
const filePath = req.url === '/' ? './index.html' : '.' + req.url;
const extname = String(path.extname(filePath)).toLowerCase();
const contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404 Not Found');
} else {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Internal Server Error');
}
} else {
res.writeHead(200, {'Content-Type': contentType});
res.end(data);
}
});
});
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});