SSR App
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import 'zone.js/dist/zone-node';
|
||||
import 'reflect-metadata';
|
||||
|
||||
import { ngExpressEngine } from '@nguniversal/express-engine';
|
||||
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
|
||||
import { resolve } from 'path';
|
||||
import { enableProdMode } from '@angular/core';
|
||||
const domino = require('domino');
|
||||
const win = domino.createWindow('');
|
||||
|
||||
global['window'] = win;
|
||||
global['document'] = win.document;
|
||||
global['alert'] = console.log
|
||||
global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;
|
||||
|
||||
export async function attachExpress(app, staticDir = "./dist") {
|
||||
const STATIC_FOLDER = resolve(process.cwd(), staticDir);
|
||||
|
||||
enableProdMode();
|
||||
|
||||
const bundle = require(staticDir + '/server/main');
|
||||
|
||||
const ServerApiService = bundle.ServerApiService
|
||||
const serviceObj = new ServerApiService()
|
||||
await serviceObj.initialize()
|
||||
|
||||
app.set('view engine', 'html');
|
||||
app.engine(
|
||||
'html',
|
||||
ngExpressEngine({
|
||||
bootstrap: bundle.AppServerModuleNgFactory,
|
||||
providers: [
|
||||
provideModuleMap(bundle.LAZY_MODULE_MAP),
|
||||
{
|
||||
provide: bundle.IApiService,
|
||||
useValue: serviceObj,
|
||||
deps: []
|
||||
}
|
||||
]
|
||||
})
|
||||
);
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
//console.log('*', req.path);
|
||||
res.render(resolve(STATIC_FOLDER, 'browser/index'), { req, res })
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user