Skip to content

Commit

Permalink
feat: generate server.routes (linnovate#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
John0x authored and PatrickJS committed Nov 20, 2016
1 parent 4eb444c commit 19e89b6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
19 changes: 11 additions & 8 deletions src/server.aot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { createEngine } from 'angular2-express-engine';
// App
import { MainModuleNgFactory } from './node.module.ngfactory';

// Routes
import { routes } from './server.routes';

// enable prod for faster renders
enableProdMode();

Expand Down Expand Up @@ -69,15 +72,15 @@ function ngApp(req, res) {
originUrl: `http://localhost:${ app.get('port') }`
});
}
// Routes with html5pushstate
// ensure routes match client-side-app

/**
* use universal for specific routes
*/
app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);
app.get('/todo', ngApp);
app.get('/todo/*', ngApp);
routes.forEach(route => {
app.get(`/${route}`, ngApp);
app.get(`/${route}/*`, ngApp);
});


app.get('*', function(req, res) {
Expand Down
16 changes: 16 additions & 0 deletions src/server.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Server-side routes. Only the listed routes support html5pushstate.
* Has to match client side routes.
*
* Index (/) route does not have to be listed here.
*
* @example
* export const routes: string[] = [
* 'home', 'about'
* ];
**/
export const routes: string[] = [
'about',
'home',
'todo',
];
20 changes: 11 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import { createEngine } from 'angular2-express-engine';
// App
import { MainModule } from './node.module';

// Routes
import { routes } from './server.routes';

// enable prod for faster renders
enableProdMode();

Expand Down Expand Up @@ -67,16 +70,15 @@ function ngApp(req, res) {
originUrl: `http://localhost:${ app.get('port') }`
});
}
// Routes with html5pushstate
// ensure routes match client-side-app
app.get('/', ngApp);
app.get('/about', ngApp);
app.get('/about/*', ngApp);
app.get('/home', ngApp);
app.get('/home/*', ngApp);
app.get('/todo', ngApp);
app.get('/todo/*', ngApp);

/**
* use universal for specific routes
*/
app.get('/', ngApp);
routes.forEach(route => {
app.get(`/${route}`, ngApp);
app.get(`/${route}/*`, ngApp);
});

app.get('*', function(req, res) {
res.setHeader('Content-Type', 'application/json');
Expand Down

0 comments on commit 19e89b6

Please sign in to comment.