简体   繁体   English

Angular Universal - NodeJS 服务器对动态路由的 HTML 的响应

[英]Angular Universal - NodeJS Server's Response to HTML for Dynamic Routes

In angular-universal Tour of Heroes application ( https://angular.io/generated/zips/universal/universal.zip ), we can use prerender script to generate static HTML for static routes only. In angular-universal Tour of Heroes application ( https://angular.io/generated/zips/universal/universal.zip ), we can use prerender script to generate static HTML for static routes only. Is there any way to print the generated HTML on NodeJS console in case of dynamic routes :dynamic routes的情况下,有什么方法可以在 NodeJS 控制台上打印生成的 HTML :

// All regular routes use the Universal engine
server.get('*', (req, res) => {
  res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});

What changes I need to do here if I want to print the generated HTML on console for the route:如果我想在控制台上打印生成的 HTML 路线,我需要在此处进行哪些更改:

http://localhost:4000/detail/12

Does this require other node js libraries?这是否需要其他节点 js 库?

Any help would be appreciated.任何帮助,将不胜感激。

Thanks.谢谢。

You can pass a callback to the render method您可以将回调传递给render方法

server.get('*', (req, res) => {
  res.render(indexHtml, { req, providers: [{ provide: 
APP_BASE_HREF, useValue: req.baseUrl }] }, (err, html)=> console.log(html));
});

See the documentation here请参阅此处的文档

https://expressjs.com/fr/api.html#res.render https://expressjs.com/fr/api.html#res.render

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM