简体   繁体   中英

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. Is there any way to print the generated HTML on NodeJS console in case of dynamic routes :

// 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:

http://localhost:4000/detail/12

Does this require other node js libraries?

Any help would be appreciated.

Thanks.

You can pass a callback to the render method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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