fork of hey-api/openapi-ts because I need some additional things
at main 1.6 kB view raw
1import { dirname, resolve } from 'node:path'; 2import { fileURLToPath } from 'node:url'; 3 4import { 5 AngularNodeAppEngine, 6 createNodeRequestHandler, 7 isMainModule, 8 writeResponseToNodeResponse, 9} from '@angular/ssr/node'; 10import express from 'express'; 11 12const serverDistFolder = dirname(fileURLToPath(import.meta.url)); 13const browserDistFolder = resolve(serverDistFolder, '../browser'); 14 15const app = express(); 16const angularApp = new AngularNodeAppEngine(); 17 18/** 19 * Example Express Rest API endpoints can be defined here. 20 * Uncomment and define endpoints as necessary. 21 * 22 * Example: 23 * ```ts 24 * app.get('/api/**', (req, res) => { 25 * // Handle API request 26 * }); 27 * ``` 28 */ 29 30/** 31 * Serve static files from /browser 32 */ 33app.use( 34 express.static(browserDistFolder, { 35 index: false, 36 maxAge: '1y', 37 redirect: false, 38 }), 39); 40 41/** 42 * Handle all other requests by rendering the Angular application. 43 */ 44app.use('/**', (req, res, next) => { 45 angularApp 46 .handle(req) 47 .then((response) => 48 response ? writeResponseToNodeResponse(response, res) : next(), 49 ) 50 .catch(next); 51}); 52 53/** 54 * Start the server if this module is the main entry point. 55 * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000. 56 */ 57if (isMainModule(import.meta.url)) { 58 const port = process.env['PORT'] || 4000; 59 app.listen(port, () => { 60 console.log(`Node Express server listening on http://localhost:${port}`); 61 }); 62} 63 64/** 65 * Request handler used by the Angular CLI (for dev-server and during build) or Firebase Cloud Functions. 66 */ 67export const reqHandler = createNodeRequestHandler(app);