Skip to content

Commit 0eb9d90

Browse files
committed
Step 3 - Finish
1 parent 2b5f23d commit 0eb9d90

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

apps/host-application/project.json

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"sourceMap": true
6363
}
6464
},
65-
"defaultConfiguration": "production"
65+
"defaultConfiguration": "development"
6666
},
6767
"serve": {
6868
"dependsOn": ["^build"],
@@ -93,10 +93,22 @@
9393
"jestConfig": "apps/host-application/jest.config.ts"
9494
}
9595
},
96+
"build-static": {
97+
"dependsOn": ["build"],
98+
"executor": "nx:run-commands",
99+
"options": {
100+
"commands": [
101+
"cp dist/apps/host-application/browser/index.csr.html dist/apps/host-application/browser/index.html",
102+
"touch dist/apps/host-application/browser/404.html"
103+
],
104+
"parallel": false
105+
}
106+
},
96107
"serve-static": {
97108
"executor": "@nx/web:file-server",
98109
"options": {
99-
"buildTarget": "host-application:build",
110+
"buildTarget": "host-application:build-static",
111+
"port": 4200,
100112
"staticFilePath": "dist/apps/host-application/browser",
101113
"spa": true
102114
}

apps/host-application/src/server.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,14 @@ import {
55
writeResponseToNodeResponse,
66
} from '@angular/ssr/node';
77
import express from 'express';
8-
import { dirname, resolve } from 'node:path';
9-
import { fileURLToPath } from 'node:url';
8+
import { dirname, join } from 'node:path';
9+
import { fileURLToPath, pathToFileURL } from 'node:url';
1010

11-
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
12-
const browserDistFolder = resolve(serverDistFolder, '../browser');
11+
const pathToSSRServer = pathToFileURL(join(dirname(fileURLToPath(import.meta.url)),'server.ssr.mjs')).toString()
1312

1413
const app = express();
1514
const angularApp = new AngularNodeAppEngine();
1615

17-
/**
18-
* Example Express Rest API endpoints can be defined here.
19-
* Uncomment and define endpoints as necessary.
20-
*
21-
* Example:
22-
* ```ts
23-
* app.get('/api/**', (req, res) => {
24-
* // Handle API request
25-
* });
26-
* ```
27-
*/
28-
29-
/**
30-
* Serve static files from /browser
31-
*/
32-
app.use(
33-
express.static(browserDistFolder, {
34-
maxAge: '1y',
35-
index: false,
36-
redirect: false,
37-
})
38-
);
39-
4016
/**
4117
* Handle all other requests by rendering the Angular application.
4218
*/
@@ -53,7 +29,7 @@ app.use('/**', (req, res, next) => {
5329
* Start the server if this module is the main entry point.
5430
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
5531
*/
56-
if (isMainModule(import.meta.url)) {
32+
if (isMainModule(pathToSSRServer)) {
5733
const port = process.env['PORT'] || 4000;
5834
app.listen(port, () => {
5935
console.log(`Node Express server listening on http://localhost:${port}`);

0 commit comments

Comments
 (0)