Mercurial
comparison react_games/backend/routes/web.ts @ 37:fb9bcd3145cb
[ReactGames] Few games I made using react just to practice few things.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 01 Dec 2025 20:22:47 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 36:84672efec192 | 37:fb9bcd3145cb |
|---|---|
| 1 import { Router, static as serveStatic } from 'express'; | |
| 2 import path from 'node:path'; | |
| 3 | |
| 4 type SpaMount = { | |
| 5 baseUrl: string; // Base path like /games | |
| 6 entryHtml: string; // we need to define its entry index html | |
| 7 }; | |
| 8 | |
| 9 export function createWebRouter(publicDir: string, spaMounts: SpaMount[]) { | |
| 10 const router = Router(); | |
| 11 | |
| 12 // order matters so geting just public one first | |
| 13 router.use(serveStatic(publicDir, { | |
| 14 index: 'index.html', | |
| 15 extensions: ['html'], | |
| 16 setHeaders: (res) => { | |
| 17 res.setHeader('X-Content-Type-Options', 'nosniff'); | |
| 18 }, | |
| 19 })); | |
| 20 | |
| 21 for (const { baseUrl, entryHtml } of spaMounts) { | |
| 22 router.get(baseUrl, (_req, res) => { | |
| 23 res.sendFile(path.join(publicDir, entryHtml)); | |
| 24 }); | |
| 25 router.get(`${baseUrl}/:rest`, (_req, res) => { | |
| 26 res.sendFile(path.join(publicDir, entryHtml)); | |
| 27 }); | |
| 28 } | |
| 29 | |
| 30 | |
| 31 router.use((_req, res) => { | |
| 32 res.status(404).send('Not Found'); | |
| 33 }); | |
| 34 | |
| 35 return router; | |
| 36 } | |
| 37 |