Skip to content

Commit 8a7a7cb

Browse files
committed
test: check if process import is result of using default server.ts
1 parent 97b690b commit 8a7a7cb

File tree

2 files changed

+81
-61
lines changed

2 files changed

+81
-61
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { APP_BASE_HREF } from '@angular/common';
2+
import { CommonEngine, isMainModule } from '@angular/ssr/node';
3+
import express from 'express';
4+
import { dirname, join, resolve } from 'node:path';
5+
import { fileURLToPath } from 'node:url';
6+
import bootstrap from './main.server';
7+
8+
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
9+
const browserDistFolder = resolve(serverDistFolder, '../browser');
10+
const indexHtml = join(serverDistFolder, 'index.server.html');
11+
12+
const app = express();
13+
const commonEngine = new CommonEngine();
14+
15+
/**
16+
* Example Express Rest API endpoints can be defined here.
17+
* Uncomment and define endpoints as necessary.
18+
*
19+
* Example:
20+
* ```ts
21+
* app.get('/api/**', (req, res) => {
22+
* // Handle API request
23+
* });
24+
* ```
25+
*/
26+
console.log('server.ts')
27+
/**
28+
* Serve static files from /browser
29+
*/
30+
app.get(
31+
'**',
32+
express.static(browserDistFolder, {
33+
maxAge: '1y',
34+
index: 'index.html'
35+
}),
36+
);
37+
38+
/**
39+
* Handle all other requests by rendering the Angular application.
40+
*/
41+
app.get('**', (req, res, next) => {
42+
const { protocol, originalUrl, baseUrl, headers } = req;
43+
44+
console.log('handling request', req.url);
45+
46+
commonEngine
47+
.render({
48+
bootstrap,
49+
documentFilePath: indexHtml,
50+
url: `${protocol}://${headers.host}${originalUrl}`,
51+
publicPath: browserDistFolder,
52+
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
53+
})
54+
.then((html) => res.send(html))
55+
.catch((err) => next(err));
56+
});
57+
58+
/**
59+
* Start the server if this module is the main entry point.
60+
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
61+
*/
62+
if (isMainModule(import.meta.url)) {
63+
const port = process.env['PORT'] || 4000;
64+
app.listen(port, () => {
65+
console.log(`Node Express server listening on http://localhost:${port}`);
66+
});
67+
}
Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,18 @@
1-
import { APP_BASE_HREF } from '@angular/common';
2-
import { CommonEngine, isMainModule } from '@angular/ssr/node';
3-
import express from 'express';
4-
import { dirname, join, resolve } from 'node:path';
5-
import { fileURLToPath } from 'node:url';
6-
import bootstrap from './main.server';
1+
import { CommonEngine } from '@angular/ssr/node'
72

8-
const serverDistFolder = dirname(fileURLToPath(import.meta.url));
9-
const browserDistFolder = resolve(serverDistFolder, '../browser');
10-
const indexHtml = join(serverDistFolder, 'index.server.html');
3+
const commonEngine = new CommonEngine()
114

12-
const app = express();
13-
const commonEngine = new CommonEngine();
5+
export default async function HttpHandler(
6+
request: Request,
7+
context: any,
8+
commonEngineRenderArgs: any,
9+
): Promise<Response> {
10+
// customize if you want to
1411

15-
/**
16-
* Example Express Rest API endpoints can be defined here.
17-
* Uncomment and define endpoints as necessary.
18-
*
19-
* Example:
20-
* ```ts
21-
* app.get('/api/**', (req, res) => {
22-
* // Handle API request
23-
* });
24-
* ```
25-
*/
26-
27-
/**
28-
* Serve static files from /browser
29-
*/
30-
app.get(
31-
'**',
32-
express.static(browserDistFolder, {
33-
maxAge: '1y',
34-
index: 'index.html'
35-
}),
36-
);
37-
38-
/**
39-
* Handle all other requests by rendering the Angular application.
40-
*/
41-
app.get('**', (req, res, next) => {
42-
const { protocol, originalUrl, baseUrl, headers } = req;
43-
44-
commonEngine
45-
.render({
46-
bootstrap,
47-
documentFilePath: indexHtml,
48-
url: `${protocol}://${headers.host}${originalUrl}`,
49-
publicPath: browserDistFolder,
50-
providers: [{ provide: APP_BASE_HREF, useValue: baseUrl }],
51-
})
52-
.then((html) => res.send(html))
53-
.catch((err) => next(err));
54-
});
55-
56-
/**
57-
* Start the server if this module is the main entry point.
58-
* The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
59-
*/
60-
if (isMainModule(import.meta.url)) {
61-
const port = process.env['PORT'] || 4000;
62-
app.listen(port, () => {
63-
console.log(`Node Express server listening on http://localhost:${port}`);
64-
});
12+
return new Response(
13+
await commonEngine.render(commonEngineRenderArgs),
14+
{
15+
headers: { 'content-type': 'text/html' }
16+
}
17+
)
6518
}

0 commit comments

Comments
 (0)