Skip to content

Instantly share code, notes, and snippets.

@cyrilletuzi
Last active August 1, 2017 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrilletuzi/ebe4e46a581ce93ad913c14733d5f10e to your computer and use it in GitHub Desktop.
Save cyrilletuzi/ebe4e46a581ce93ad913c14733d5f10e to your computer and use it in GitHub Desktop.
'use strict';
/* Server specific version of Zone.js */
require('zone.js/dist/zone-node');
const express = require('express');
const ngUniversal = require('@nguniversal/express-engine');
/* The server bundle is loaded here, it's why you don't want a changing hash in it */
const appServer = require('./dist-server/main.bundle');
/* Server-side rendering */
function angularRouter(req, res) {
/* Server-side rendering */
res.render('index', { req, res });
}
const app = express();
/* Root route before static files, or it will serve a static index.html, without pre-rendering */
app.get('/', angularRouter);
/* Serve the static files generated by the CLI (index.html, CSS? JS, assets...) */
app.use(express.static(`${__dirname}/dist`));
/* Configure Angular Express engine */
app.engine('html', ngUniversal.ngExpressEngine({
bootstrap: appServer.AppServerModuleNgFactory
}));
app.set('view engine', 'html');
app.set('views', 'dist');
/* Direct all routes to index.html, where Angular will take care of routing */
app.get('*', angularRouter);
app.listen(3000, () => {
console.log(`Listening on http://localhost:3000`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment