1 |
50428
|
argiro.kok
|
import 'zone.js/dist/zone-node';
|
2 |
|
|
import 'reflect-metadata';
|
3 |
|
|
import { renderModuleFactory } from '@angular/platform-server';
|
4 |
|
|
import { enableProdMode } from '@angular/core';
|
5 |
|
|
|
6 |
|
|
import * as express from 'express';
|
7 |
|
|
import { join } from 'path';
|
8 |
|
|
import { readFileSync } from 'fs';
|
9 |
|
|
|
10 |
|
|
// Faster server renders w/ Prod mode (dev mode never needed)
|
11 |
|
|
enableProdMode();
|
12 |
|
|
|
13 |
|
|
// Express server
|
14 |
|
|
const app = express();
|
15 |
|
|
|
16 |
|
|
const PORT = process.env.PORT || 4000;
|
17 |
|
|
const DIST_FOLDER = join(process.cwd(), 'dist');
|
18 |
|
|
|
19 |
|
|
// Our index.html we'll use as our template
|
20 |
|
|
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString();
|
21 |
|
|
|
22 |
|
|
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
|
23 |
55959
|
konstantin
|
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
|
24 |
50428
|
argiro.kok
|
|
25 |
|
|
// Express Engine
|
26 |
55959
|
konstantin
|
import {ngExpressEngine, RenderOptions} from '@nguniversal/express-engine';
|
27 |
50428
|
argiro.kok
|
// Import module map for lazy loading
|
28 |
|
|
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader';
|
29 |
|
|
|
30 |
|
|
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
|
31 |
54949
|
argiro.kok
|
// app.engine('html', ngExpressEngine({
|
32 |
|
|
// bootstrap: AppServerModuleNgFactory,
|
33 |
|
|
// providers: [
|
34 |
|
|
// { provide: 'request', useFactory: () => options.req, deps: [] },
|
35 |
|
|
// provideModuleMap(LAZY_MODULE_MAP)
|
36 |
|
|
// ]
|
37 |
|
|
// }));
|
38 |
50428
|
argiro.kok
|
|
39 |
55271
|
argiro.kok
|
// be able to get request and get domain from there
|
40 |
55959
|
konstantin
|
app.engine('html', (_, options: RenderOptions, callback) => {
|
41 |
54949
|
argiro.kok
|
let engine = ngExpressEngine({
|
42 |
|
|
bootstrap: AppServerModuleNgFactory,
|
43 |
|
|
providers: [
|
44 |
|
|
{ provide: 'request', useFactory: () => options.req, deps: [] },
|
45 |
|
|
provideModuleMap(LAZY_MODULE_MAP)
|
46 |
|
|
]
|
47 |
|
|
});
|
48 |
|
|
engine(_, options, callback);
|
49 |
|
|
});
|
50 |
|
|
|
51 |
50428
|
argiro.kok
|
app.set('view engine', 'html');
|
52 |
|
|
app.set('views', join(DIST_FOLDER, 'browser'));
|
53 |
|
|
|
54 |
|
|
/* - Example Express Rest API endpoints -
|
55 |
|
|
app.get('/api/**', (req, res) => { });
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
// Server static files from /browser
|
59 |
|
|
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
|
60 |
|
|
maxAge: '1y'
|
61 |
|
|
}));
|
62 |
|
|
|
63 |
|
|
// ALl regular routes use the Universal engine
|
64 |
|
|
app.get('*', (req, res) => {
|
65 |
|
|
res.render('index', { req });
|
66 |
|
|
});
|
67 |
|
|
|
68 |
|
|
// Start up the Node server
|
69 |
|
|
app.listen(PORT, () => {
|
70 |
|
|
console.log(`Node Express server listening on http://localhost:${PORT}`);
|
71 |
|
|
});
|