← back to Catalog Crawl Atlas
server.js
16 lines
const http=require('http'), fs=require('fs'), path=require('path');
const ROOT=path.join(__dirname,'public'), DATA=path.join(__dirname,'data');
const USER='admin', PASS='DW2024!';
const MIME={'.html':'text/html','.js':'text/javascript','.json':'application/json','.css':'text/css'};
const srv=http.createServer((req,res)=>{
const auth=(req.headers.authorization||'').split(' ')[1]||'';
const [u,p]=Buffer.from(auth,'base64').toString().split(':');
if(u!==USER||p!==PASS){res.writeHead(401,{'WWW-Authenticate':'Basic realm="Crawl Atlas"'});return res.end('auth required');}
let f=req.url.split('?')[0]; if(f==='/')f='/index.html';
let fp = (f==='/graph.json'||f==='/products.json') ? path.join(DATA,path.basename(f)) : path.join(ROOT,f);
fs.readFile(fp,(e,buf)=>{ if(e){res.writeHead(404);return res.end('not found');}
res.writeHead(200,{'Content-Type':MIME[path.extname(fp)]||'application/octet-stream','Cache-Control':'no-store'});res.end(buf);});
});
const PORT=process.env.PORT||9791;
srv.listen(PORT,()=>console.log(`Crawl Atlas → http://127.0.0.1:${PORT} (admin / DW2024!)`));