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 |
|
|
|
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 |
|
|
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
|
24 |
|
|
|
25 |
|
|
// Express Engine
|
26 |
|
|
import { ngExpressEngine } from '@nguniversal/express-engine';
|
27 |
|
|
// 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 |
|
|
app.engine('html', ngExpressEngine({
|
32 |
|
|
bootstrap: AppServerModuleNgFactory,
|
33 |
|
|
providers: [
|
34 |
|
|
provideModuleMap(LAZY_MODULE_MAP)
|
35 |
|
|
]
|
36 |
|
|
}));
|
37 |
|
|
|
38 |
|
|
app.set('view engine', 'html');
|
39 |
|
|
app.set('views', join(DIST_FOLDER, 'browser'));
|
40 |
|
|
|
41 |
|
|
/* - Example Express Rest API endpoints -
|
42 |
|
|
app.get('/api/**', (req, res) => { });
|
43 |
|
|
*/
|
44 |
|
|
|
45 |
|
|
// Server static files from /browser
|
46 |
|
|
app.get('*.*', express.static(join(DIST_FOLDER, 'browser'), {
|
47 |
|
|
maxAge: '1y'
|
48 |
|
|
}));
|
49 |
|
|
|
50 |
|
|
// ALl regular routes use the Universal engine
|
51 |
|
|
app.get('*', (req, res) => {
|
52 |
|
|
res.render('index', { req });
|
53 |
|
|
});
|
54 |
|
|
|
55 |
|
|
// Start up the Node server
|
56 |
|
|
app.listen(PORT, () => {
|
57 |
|
|
console.log(`Node Express server listening on http://localhost:${PORT}`);
|
58 |
|
|
});
|