Files
giochino/server.js
T

42 lines
1.0 KiB
JavaScript

const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 8090;
const ROOT = '/home/codex/giochino';
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.json': 'application/json'
};
const server = http.createServer((req, res) => {
let filePath = path.join(ROOT, req.url === '/' ? 'index.html' : req.url);
const ext = path.extname(filePath);
const contentType = MIME[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404 Not Found: ' + req.url);
return;
}
res.writeHead(200, {'Content-Type': contentType});
res.end(data);
});
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Server attivo su http://0.0.0.0:${PORT}/`);
console.log(`Cartella: ${ROOT}`);
});
server.on('error', (err) => {
console.error('Errore server:', err.message);
});