50 lines
1.3 KiB
JavaScript
50 lines
1.3 KiB
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 8081;
|
|
|
|
const mimeTypes = {
|
|
'.html': 'text/html',
|
|
'.js': 'text/javascript',
|
|
'.css': 'text/css',
|
|
'.json': 'application/json',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpg',
|
|
'.gif': 'image/gif',
|
|
'.svg': 'image/svg+xml',
|
|
'.ico': 'image/x-icon'
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
console.log(`${req.method} ${req.url}`);
|
|
|
|
let filePath = '.' + req.url;
|
|
if (filePath === './') {
|
|
filePath = './index.html';
|
|
}
|
|
|
|
const extname = String(path.extname(filePath)).toLowerCase();
|
|
const contentType = mimeTypes[extname] || 'application/octet-stream';
|
|
|
|
fs.readFile(filePath, (error, content) => {
|
|
if (error) {
|
|
if (error.code === 'ENOENT') {
|
|
res.writeHead(404, { 'Content-Type': 'text/html' });
|
|
res.end('<h1>404 Not Found</h1>', 'utf-8');
|
|
} else {
|
|
res.writeHead(500);
|
|
res.end('Server Error: ' + error.code, 'utf-8');
|
|
}
|
|
} else {
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
res.end(content, 'utf-8');
|
|
}
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`[TraePreviewUrl]: http://localhost:${PORT}`);
|
|
console.log(`Server running at http://localhost:${PORT}/`);
|
|
});
|