如何在Express中输出漂亮的html? [英] How to output pretty html in Express?

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

问题描述

我注意到,在使用Express for Node.js时,它输出的HTML代码没有任何换行符或标签。它不是很漂亮,虽然它在技术上可以更有效地下载。



如何获得打印好的格式化HTML?

解决方案

在你的主要 app.js 或它的地方是什么:



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('查看选项',{pretty:true});
});

我把这个漂亮的打印放在开发中,因为您将需要在生产中使用丑来提高效率。在生产中部署时,请确保设置环境变量 NODE_ENV = production 。这可以在 sh 脚本中使用,您在 package.json 的脚本字段中使用并执行启动。



Express 3 已更改,因为:


视图选项设置已不再需要,应用程序。本地人是与res.render()的合并的局部变量,所以[app.locals.pretty = true与传递res.render(view,{pretty:true})相同。



I've noticed that while using Express for Node.js, it outputs the html code without any newline characters or tabs. It's not very pretty, although it could technically be more efficient for downloading.

How could I get it to print nice formatted html?

解决方案

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 });
});

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 changed this because:

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天全站免登陆