UNPKG

@nx/angular-rsbuild

Version:

Rsbuild Plugin for building Angular.

50 lines (49 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createServer = createServer; const tslib_1 = require("tslib"); const common_1 = require("@angular/common"); const node_1 = require("@angular/ssr/node"); const express_1 = tslib_1.__importDefault(require("express")); const node_path_1 = require("node:path"); function createServer(bootstrap, opts) { const serverDistFolder = opts?.serverDistFolder ?? (0, node_path_1.dirname)(__filename); const browserDistFolder = opts?.browserDistFolder ?? (0, node_path_1.resolve)(serverDistFolder, '../browser'); const staticFolder = opts?.staticFolder ?? (0, node_path_1.resolve)(browserDistFolder, 'static'); const indexHtml = opts?.indexHtml ?? (0, node_path_1.join)(browserDistFolder, 'index.html'); const app = (0, express_1.default)(); const commonEngine = new node_1.CommonEngine(); app.get('**', express_1.default.static(browserDistFolder, { maxAge: '1y', index: false, })); app.use('/static', express_1.default.static(staticFolder)); /** * Handle all other requests by rendering the Angular application. */ app.get('**', async (req, res, next) => { const { protocol, originalUrl, baseUrl, headers } = req; commonEngine .render({ bootstrap, documentFilePath: indexHtml, url: `${protocol}://${headers.host}${originalUrl}`, publicPath: browserDistFolder, providers: [{ provide: common_1.APP_BASE_HREF, useValue: baseUrl }], }) .then((html) => { res.send(html); }) .catch((err) => next(err)); }); return { app, listen: (port = process.env['PORT'] ? Number.parseInt(process.env['PORT'], 10) : 4000) => { app.listen(port, () => { console.log(`Node Express server listening on http://localhost:${port}`); }); }, }; }