1- import {
2- AngularNodeAppEngine ,
3- createNodeRequestHandler ,
4- isMainModule ,
5- writeResponseToNodeResponse ,
6- } from '@angular/ssr/node' ;
1+ import { isMainModule } from '@angular/ssr/node' ;
72import express from 'express' ;
83import { dirname , resolve } from 'node:path' ;
94import { fileURLToPath } from 'node:url' ;
5+ import { getContext } from '@netlify/angular-runtime/context' ;
6+ import { AngularAppEngine , createRequestHandler } from '@angular/ssr' ;
107
118const serverDistFolder = dirname ( fileURLToPath ( import . meta. url ) ) ;
129const browserDistFolder = resolve ( serverDistFolder , '../browser' ) ;
1310
1411const app = express ( ) ;
15- const angularApp = new AngularNodeAppEngine ( ) ;
12+ const angularAppEngine = new AngularAppEngine ( ) ;
1613
1714/**
1815 * Example Express Rest API endpoints can be defined here.
@@ -37,18 +34,6 @@ app.use(
3734 } )
3835) ;
3936
40- /**
41- * Handle all other requests by rendering the Angular application.
42- */
43- app . use ( '/**' , ( req , res , next ) => {
44- angularApp
45- . handle ( req )
46- . then ( response =>
47- response ? writeResponseToNodeResponse ( response , res ) : next ( )
48- )
49- . catch ( next ) ;
50- } ) ;
51-
5237/**
5338 * Start the server if this module is the main entry point.
5439 * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
@@ -60,7 +45,16 @@ if (isMainModule(import.meta.url)) {
6045 } ) ;
6146}
6247
48+ export async function netlifyAppEngineHandler (
49+ request : Request
50+ ) : Promise < Response > {
51+ const context = getContext ( ) ;
52+
53+ const result = await angularAppEngine . handle ( request , context ) ;
54+ return result || new Response ( 'Not found' , { status : 404 } ) ;
55+ }
56+
6357/**
6458 * The request handler used by the Angular CLI (dev-server and during build).
6559 */
66- export const reqHandler = createNodeRequestHandler ( app ) ;
60+ export const reqHandler = createRequestHandler ( netlifyAppEngineHandler ) ;
0 commit comments