Files

17 lines
1.0 KiB
JavaScript

import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
const host='192.168.3.74',port=17852;
const server=http.createServer((req,res)=>{
const route=new URL(req.url,'http://localhost').pathname;
if(!['/','/index.html','/SCORES.html','/DIFFICULTY.html','/DESIGN.html'].includes(route)){res.writeHead(404,{'Content-Type':'text/plain; charset=utf-8'});res.end('Not found');return;}
if(!['GET','HEAD'].includes(req.method)){res.writeHead(405);res.end();return;}
const html=fs.readFileSync(path.join(root,route==='/'?'index.html':route.slice(1)));
res.writeHead(200,{'Content-Type':'text/html; charset=utf-8','Cache-Control':'no-store','Content-Length':html.length});
res.end(req.method==='HEAD'?undefined:html);
});
server.listen(port,host,()=>console.log(`NumberSwipe DEMO: http://${host}:${port}/`));
server.on('error',error=>{console.error(error.message);process.exitCode=1;});