Skip to content

Commit

Permalink
Copy main.server.ts from FrozenPandaz PR to universal-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
intellix committed Mar 25, 2017
1 parent c2ea1eb commit 0159223
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/app/app.browser.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { AppComponent } from './app.component';
],
imports: [
BrowserModule.withServerTransition({
appId: 'app-root-server',
appId: 'angular-cli-server',
}),
FormsModule,
HttpModule
Expand Down
35 changes: 32 additions & 3 deletions src/main.server.ts
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}`);
});
50 changes: 50 additions & 0 deletions src/ng-express-engine.ts
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();
});
});
}
4 changes: 3 additions & 1 deletion src/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"outDir": "../out-tsc/app",
"module": "es2015",
"baseUrl": "",
"types": []
"types": [
"node"
]
},
"exclude": [
"test.ts",
Expand Down

0 comments on commit 0159223

Please sign in to comment.