如何让Express输出格式正确的HTML? [英] How can I get Express to output nicely formatted HTML?
问题描述
使用Express for Node.js时,我注意到它输出的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 ,{pretty:true}).
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屋!