29 lines
814 B
JavaScript
29 lines
814 B
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const filePath = req.url === '/' ? 'index.html' : req.url.slice(1);
|
|
const ext = path.extname(filePath);
|
|
const mimeTypes = {
|
|
'.html': 'text/html',
|
|
'.js': 'application/javascript',
|
|
'.css': 'text/css'
|
|
};
|
|
|
|
fs.readFile(path.join(process.cwd(), filePath), (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404);
|
|
res.end('Not Found');
|
|
return;
|
|
}
|
|
res.writeHead(200, { 'Content-Type': mimeTypes[ext] || 'text/plain' });
|
|
res.end(data);
|
|
});
|
|
});
|
|
|
|
server.listen(0, () => {
|
|
const port = server.address().port;
|
|
console.log('Server running on http://localhost:' + port);
|
|
});
|