如何让 Express 输出格式良好的 HTML? [英] How can I get Express to output nicely formatted HTML?

查看:26
本文介绍了如何让 Express 输出格式良好的 HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Node.js 中使用 Express 时,我注意到它输出的 HTML 代码没有任何换行符或制表符.虽然下载可能更高效,但在开发过程中可读性不高.

When using Express for Node.js, I noticed that it outputs the HTML code without any newline characters or tabs. Though it may be more efficient to download, it's not very readable during development.

如何让 Express 输出格式良好的 HTML?

How can I get Express to output nicely formatted HTML?

推荐答案

在你的主要 app.js 或它的位置:

In your main app.js or what is in it's place:

Express 4.x

if (app.get('env') === 'development') {
  app.locals.pretty = true;
}

Express 3.x

app.configure('development', function(){
  app.use(express.errorHandler());
  app.locals.pretty = true;
});

Express 2.x

app.configure('development', function(){
  app.use(express.errorHandler());
  app.set('view options', { pretty: true });
});

我在 development 中放置了漂亮的打印,因为您会希望在 production 中使用丑陋"来提高效率.在生产环境中部署时,请确保设置环境变量 NODE_ENV=production.这可以通过您在 package.json 的脚本"字段中使用的 sh 脚本来完成,并执行以启动.

I put the pretty print in development because you'll want more efficiency with the 'ugly' in production. Make sure to set environment variable NODE_ENV=production when you're deploying in production. This can be done with an sh script you use in the 'script' field of package.json and executed to start.

Express 3 改变了这个因为:

Express 3 changed this because:

不再需要视图选项"设置,app.locals是与res.render()合并的局部变量,所以[app.locals.pretty = true就等于传递res.render(view, { 漂亮: 真实 }).

The "view options" setting is no longer necessary, app.locals are the local variables merged with res.render()'s, so [app.locals.pretty = true is the same as passing res.render(view, { pretty: true }).

这篇关于如何让 Express 输出格式良好的 HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆