21 lines
710 B
JavaScript
21 lines
710 B
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 8095;
|
|
const ROOT = '/home/codex/giochino';
|
|
|
|
const MIME = { '.html': 'text/html', '.js': 'application/javascript', '.css': 'text/css', '.png': 'image/png' };
|
|
|
|
const server = http.createServer((req, res) => {
|
|
let filePath = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
|
|
const ext = path.extname(filePath);
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) { res.writeHead(404); res.end('404'); return; }
|
|
res.writeHead(200, { 'Content-Type': MIME[ext] || 'text/plain' });
|
|
res.end(data);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, '0.0.0.0', () => console.log(`Server: http://0.0.0.0:${PORT}/`));
|