diff --git a/src/server.aot.ts b/src/server.aot.ts index c017c59607..b7bf32a491 100644 --- a/src/server.aot.ts +++ b/src/server.aot.ts @@ -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(); @@ -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) { diff --git a/src/server.routes.ts b/src/server.routes.ts new file mode 100644 index 0000000000..88abfe877b --- /dev/null +++ b/src/server.routes.ts @@ -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', +]; diff --git a/src/server.ts b/src/server.ts index b7bbade71a..9add8aa1e5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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(); @@ -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');