← back to Dtd Approval Viewer

server.js

17 lines

const http=require('http'),fs=require('fs'),path=require('path');
const USER='admin',PASS='DW2024!',DIR=__dirname;
const PORT=process.env.PORT||9791;
const TYPES={'.html':'text/html','.json':'application/json','.js':'text/javascript','.css':'text/css'};
http.createServer((req,res)=>{
  const auth=req.headers.authorization||'';
  const [u,p]=Buffer.from(auth.split(' ')[1]||'',
    'base64').toString().split(':');
  if(u!==USER||p!==PASS){res.writeHead(401,{'WWW-Authenticate':'Basic realm="DTD"'});return res.end('auth required')}
  let f=decodeURIComponent((req.url.split('?')[0])||'/');
  if(f==='/'||f==='')f='/index.html';
  const fp=path.join(DIR,path.normalize(f).replace(/^\/+/,''));
  if(!fp.startsWith(DIR)||!fs.existsSync(fp)){res.writeHead(404);return res.end('not found')}
  res.writeHead(200,{'Content-Type':TYPES[path.extname(fp)]||'application/octet-stream'});
  fs.createReadStream(fp).pipe(res);
}).listen(PORT,'0.0.0.0',()=>console.log('DTD viewer on http://127.0.0.1:'+PORT+' (admin/DW2024!)'));