UNPKG

2.08 kBJavaScriptView Raw
1import { CommonEngine } from '@nguniversal/common/engine';
2import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
3
4/**
5 * This is an express engine for handling Angular Applications
6 */
7function ngExpressEngine(setupOptions) {
8 const engine = new CommonEngine(setupOptions.bootstrap, setupOptions.providers);
9 return function (filePath, options, callback) {
10 try {
11 const renderOptions = { ...options };
12 if (!setupOptions.bootstrap && !renderOptions.bootstrap) {
13 throw new Error('You must pass in a NgModule to be bootstrapped');
14 }
15 const { req } = renderOptions;
16 const res = renderOptions.res ?? req.res;
17 renderOptions.url =
18 renderOptions.url ?? `${req.protocol}://${req.get('host') || ''}${req.baseUrl}${req.url}`;
19 renderOptions.documentFilePath = renderOptions.documentFilePath ?? filePath;
20 renderOptions.providers = [...(renderOptions.providers ?? []), getReqResProviders(req, res)];
21 // eslint-disable-next-line @typescript-eslint/no-unused-expressions
22 renderOptions.publicPath =
23 renderOptions.publicPath ?? setupOptions.publicPath ?? options.settings?.views;
24 renderOptions.inlineCriticalCss =
25 renderOptions.inlineCriticalCss ?? setupOptions.inlineCriticalCss;
26 engine
27 .render(renderOptions)
28 .then((html) => callback(null, html))
29 .catch(callback);
30 }
31 catch (err) {
32 callback(err);
33 }
34 };
35}
36/**
37 * Get providers of the request and response
38 */
39function getReqResProviders(req, res) {
40 const providers = [
41 {
42 provide: REQUEST,
43 useValue: req,
44 },
45 ];
46 if (res) {
47 providers.push({
48 provide: RESPONSE,
49 useValue: res,
50 });
51 }
52 return providers;
53}
54
55/**
56 * Generated bundle index. Do not edit.
57 */
58
59export { ngExpressEngine };
60//# sourceMappingURL=express-engine.mjs.map