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