Created
July 15, 2017 01:00
-
-
Save evertonrobertoauler/3c208d3f2bf7bf68748eb569f58bed79 to your computer and use it in GitHub Desktop.
src/server.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'reflect-metadata'; | |
import 'zone.js/dist/zone-node'; | |
import { renderModuleFactory } from '@angular/platform-server' | |
import { enableProdMode } from '@angular/core' | |
import { AppServerModuleNgFactory } from '../dist/ngfactory/src/app/app.server.module.ngfactory' | |
import * as express from 'express'; | |
import { readFileSync } from 'fs'; | |
import { join } from 'path'; | |
const PORT = process.env.PORT || 4000; | |
enableProdMode(); | |
const app = express(); | |
let template = readFileSync(join(__dirname, '..', 'dist', 'index.html')).toString(); | |
app.engine('html', (_, options, callback) => { | |
const opts = { | |
document: template, | |
url: options.req.url, | |
extraProviders: [ | |
{ provide: 'request', useFactory: () => options.req } | |
] | |
}; | |
renderModuleFactory(AppServerModuleNgFactory, opts) | |
.then(html => callback(null, html)); | |
}); | |
app.set('view engine', 'html'); | |
app.set('views', 'src') | |
app.get('*.*', express.static(join(__dirname, '..', 'dist'))); | |
app.get('*', (req, res) => { | |
res.render('index', { req }); | |
}); | |
app.listen(PORT, () => { | |
console.log(`listening on http://localhost:${PORT}!`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment