仅限某些路线的角度通用渲染

时间:2017-04-18 05:56:00

标签: javascript angular angular-universal

我正在使用角度通用,但无法找到仅对主页等某些页面使用服务器端渲染的选项,并以标准角度方式渲染所有其他路径。我不想将服务器端渲染用于不需要SEO的私有页面。我可以像这样配置快递路线

// send all requests to Angular Universal
// if you want Express to handle certain routes (ex. for an API) make sure you adjust this
app.get('/', ngApp);
app.get('/home', ngApp);
app.get('/about', ngApp);

理想情况下,我根本不想了解NodeJs并在角度路由配置上配置像serverSide这样的属性:true

const appRoutes: Routes = [
  //public route, I want server rendering for SEO
  { path: 'home', component: HomeComponent, serverSide: true },
  //private user profile page, SEO is not needed
  { path: 'user/profile/:id', component: UserProfileComponent },
];

1 个答案:

答案 0 :(得分:1)

在你的server.ts中 对于您不想渲染的路线,只需执行以下操作

app.get('/api/**', (req, res) => { });

app.get('/app/**', (req, res) => {
  console.log('not rendering  app pages');
});

app.get('/auth/**', (req, res) => {
  console.log('not rendering auth page');
});

//所有常规路线都使用通用引擎

app.get('*', (req, res) => {
  res.render('index', { req });
});

希望这有帮助。