简体   繁体   English

如何从Node.js中的JSON输出中修剪空格?

[英]How to trim whitespaces from JSON output in Node.js?

On Node.js using Express, I am outputting JSON with a simple response.send(myObject) It's working fine, but the response has a lot of white spaces indentations. 在使用Express的Node.js上,我通过简单的response.send(myObject)输出JSON。它工作正常,但响应中有很多空格缩进。 Removing them reduces the size by half. 删除它们可将尺寸减小一半。 Is there a quick/proper way to do this? 有快速/正确的方法来做到这一点吗? I tried using JSON.stringify and it works great, but that turns the response type to text/html 我尝试使用JSON.stringify ,效果很好,但是将响应类型转换为text/html

a config setting was introduced in 3.0.0alpha1 3.0.0alpha1中引入了配置设置

try app.set('json spaces',0) 尝试app.set('json空格',0)

您还可以压缩输出,通过gzip压缩将删除不必要的空格。

  app.use(express.compress());

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

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