Monorepo for Aesthetic.Computer aesthetic.computer
at main 67 lines 2.2 kB view raw
1#!/usr/bin/env node 2import { createServer } from 'node:http'; 3import { readFile } from 'node:fs/promises'; 4import { extname, join, dirname } from 'node:path'; 5import { fileURLToPath } from 'node:url'; 6 7const __dirname = dirname(fileURLToPath(import.meta.url)); 8const root = join(__dirname, '..'); 9const port = Number(process.env.PORT) || 4177; 10 11const routes = new Map([ 12 ['/', 'landing-page.html'], 13 ['/index.html', 'landing-page.html'], 14 ['/user.html', 'user-page.html'], 15 ['/test-user-pages.html', 'test-user-pages.html'], 16]); 17 18const contentTypes = { 19 '.html': 'text/html; charset=utf-8', 20 '.css': 'text/css; charset=utf-8', 21 '.js': 'text/javascript; charset=utf-8', 22 '.mjs': 'text/javascript; charset=utf-8', 23 '.json': 'application/json; charset=utf-8', 24 '.svg': 'image/svg+xml', 25 '.png': 'image/png', 26 '.jpg': 'image/jpeg', 27 '.jpeg': 'image/jpeg', 28}; 29 30const server = createServer(async (req, res) => { 31 try { 32 const url = new URL(req.url || '/', `http://${req.headers.host}`); 33 34 if (url.pathname === '/user') { 35 res.statusCode = 302; 36 res.setHeader('Location', '/user.html' + url.search); 37 res.end(); 38 return; 39 } 40 41 const routeFile = routes.get(url.pathname); 42 if (routeFile) { 43 const filePath = join(root, routeFile); 44 const data = await readFile(filePath); 45 const ext = extname(filePath); 46 res.statusCode = 200; 47 res.setHeader('Content-Type', contentTypes[ext] || 'application/octet-stream'); 48 res.end(data); 49 return; 50 } 51 52 res.statusCode = 404; 53 res.setHeader('Content-Type', 'text/plain; charset=utf-8'); 54 res.end('Not found'); 55 } catch (error) { 56 res.statusCode = 500; 57 res.setHeader('Content-Type', 'text/plain; charset=utf-8'); 58 res.end(`Server error: ${error.message}`); 59 } 60}); 61 62server.listen(port, () => { 63 console.log(`🧪 User pages dev server running at http://localhost:${port}`); 64 console.log('→ Landing page: http://localhost:' + port + '/'); 65 console.log('→ User page: http://localhost:' + port + '/user.html?handle=jeffrey.at.aesthetic.computer'); 66 console.log('→ Test harness: http://localhost:' + port + '/test-user-pages.html'); 67});