Created
April 27, 2018 15:54
-
-
Save mitnick78/9a448367ab218899f193cad70f8f7904 to your computer and use it in GitHub Desktop.
ssr wiht hapijs
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 { enableProdMode } from '@angular/core'; | |
import { renderModuleFactory } from '@angular/platform-server'; | |
import { ngHapiEngine, RenderOptions } from '@nguniversal/hapi-engine'; | |
import * as Boom from 'boom'; | |
import { readFileSync } from 'fs'; | |
import { Base_Reply, Request, Server } from 'hapi'; | |
import { join } from 'path'; | |
import 'reflect-metadata'; | |
import 'zone.js/dist/zone-node'; | |
// Faster server renders w/ Prod mode (dev mode never needed) | |
enableProdMode(); | |
const PORT = process.env.PORT || 4000; | |
const DIST_FOLDER = join(process.cwd(), 'dist'); | |
// Our index.html we'll use as our template | |
const template = readFileSync(join(DIST_FOLDER, 'browser', 'index.html')).toString(); | |
// * NOTE :: leave this as require() since this file is built Dynamically from webpack | |
const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle'); | |
// Import module map for lazy loading | |
import { provideModuleMap } from '@nguniversal/module-map-ngfactory-loader'; | |
const server = new Server(); | |
server.connection({ | |
host: 'localhost', | |
port: PORT | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/{path*}', | |
handler: (req: Request, reply: any ) => { | |
ngHapiEngine({ | |
bootstrap: AppServerModuleNgFactory, | |
providers: [ | |
provideModuleMap(LAZY_MODULE_MAP) | |
], | |
req | |
}).then(html => reply(html)) | |
.then(err => reply(Boom.wrap(err))); | |
} | |
}); | |
server.start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment