-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy main.server.ts from FrozenPandaz PR to universal-cli
- Loading branch information
Showing
4 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,40 @@ | ||
import { platformDynamicServer } from '@angular/platform-server'; | ||
import { enableProdMode } from '@angular/core'; | ||
import 'zone.js/dist/zone-node'; | ||
|
||
import * as express from 'express'; | ||
import { AppServerModule } from './app/app.server.module'; | ||
// TODO: I think this is being added to core | ||
import { ngExpressEngine } from './ng-express-engine'; | ||
|
||
import { enableProdMode } from '@angular/core'; | ||
import { environment } from './environments/environment'; | ||
|
||
if (environment.production) { | ||
enableProdMode(); | ||
} | ||
|
||
platformDynamicServer().bootstrapModule(AppServerModule); | ||
const app = express(); | ||
const port = 8000; | ||
const baseUrl = `http://localhost:${port}`; | ||
|
||
// TODO: We might need to alter @ngtools/webpack | ||
app.engine('html', ngExpressEngine({ | ||
baseUrl: baseUrl, | ||
bootstrap: [AppServerModule] | ||
})); | ||
|
||
app.set('view engine', 'html'); | ||
app.set('views', 'src'); | ||
|
||
app.use('/', express.static('dist', { index: false })); | ||
|
||
[].forEach(route => { | ||
app.get(route, (req, res) => { | ||
res.render('index', { | ||
req: req | ||
}); | ||
}); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Listening at ${baseUrl}`); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
|
||
/** For now this is from https://github.com/FrozenPandaz/ng-universal-demo | ||
* Hm maybe this can be a package in @ngtools? | ||
*/ | ||
|
||
const fs = require('fs'); | ||
import { Request } from 'express'; | ||
import { NgModuleFactory, NgZone, Type, ApplicationRef } from '@angular/core'; | ||
import { platformServer, PlatformState, INITIAL_CONFIG } from '@angular/platform-server'; | ||
|
||
const templateCache = {}; | ||
export function ngExpressEngine(setupOptions) { | ||
|
||
return function (filePath, options, callback) { | ||
const moduleFactory = setupOptions.bootstrap[0]; | ||
const document = templateCache[filePath] || | ||
(templateCache[filePath] = fs.readFileSync(filePath)); | ||
|
||
handleRequest(options.req, document, moduleFactory, callback); | ||
}; | ||
} | ||
|
||
function handleRequest( | ||
req: Request, document: string, module: Type<{}>, | ||
callback: (err, html) => any) { | ||
|
||
const platform = platformServer([ | ||
{ | ||
provide: INITIAL_CONFIG, | ||
useValue: { | ||
document: document, | ||
url: req.url | ||
} | ||
} | ||
]); | ||
|
||
platform.bootstrapModule(module) | ||
.then(moduleRef => { | ||
const state = moduleRef.injector.get(PlatformState); | ||
const appRef = moduleRef.injector.get(ApplicationRef); | ||
|
||
appRef.isStable | ||
.filter((isStable: boolean) => isStable) | ||
.first() | ||
.subscribe((stable) => { | ||
callback(null, state.renderToString()); | ||
platform.destroy(); | ||
}); | ||
}); | ||
} |
This file contains 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