-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
53 lines (40 loc) · 1.48 KB
/
server.ts
1
// These are important and needed before anything elseimport 'zone.js/dist/zone-node';import 'reflect-metadata';import {enableProdMode} from '@angular/core';import * as express from 'express';import {join} from 'path';// Faster server renders w/ Prod mode (dev mode never needed)enableProdMode();// Express serverconst app = express();const PORT = process.env.PORT || 4000;const DIST_FOLDER = join(process.cwd(), 'dist');// * NOTE :: leave this as require() since this file is built Dynamically from webpackconst {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');// Express Engineimport {ngExpressEngine} from '@nguniversal/express-engine';// Import module map for lazy loadingimport {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';app.engine('html', ngExpressEngine({ bootstrap: AppServerModuleNgFactory, providers: [ provideModuleMap(LAZY_MODULE_MAP) ]}));app.set('view engine', 'html');app.set('views', join(DIST_FOLDER, 'browser'));// TODO: implement data requests securelyapp.get('/api/*', (req, res) => { res.status(404).send('data requests are not supported');});// Server static files from /browserapp.get('*.*', express.static(join(DIST_FOLDER, 'browser')));// All regular routes use the Universal engineapp.get('*', (req, res) => { res.render('index', {req});});// Start up the Node serverapp.listen(PORT, () => { console.log(`Node server listening on http://localhost:${PORT}`);});