Node.js HTTP server serving a canvas Snake game optimised for phones: inline onclick handlers for reliable touch, swipe support, big d-pad, no-cache headers, and responsive canvas sizing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
845 B
JavaScript
38 lines
845 B
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 10000;
|
|
|
|
const MIME = {
|
|
'.html': 'text/html',
|
|
'.js': 'text/javascript',
|
|
'.css': 'text/css',
|
|
'.ico': 'image/x-icon',
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
let filePath = req.url === '/' ? '/index.html' : req.url;
|
|
filePath = path.join(__dirname, 'public', filePath);
|
|
|
|
const ext = path.extname(filePath);
|
|
const mime = MIME[ext] || 'text/plain';
|
|
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
return;
|
|
}
|
|
res.writeHead(200, { 'Content-Type': mime, 'Cache-Control': 'no-store' });
|
|
res.end(data);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`Snake running at http://localhost:${PORT}`);
|
|
});
|