【问题标题】:JSON formatting in iisnodeiisnode 中的 JSON 格式
【发布时间】:2013-03-11 17:36:08
【问题描述】:

在本地运行我的 node.js 应用程序时,我会得到格式良好的带有换行符和空格的 JSON 输出,如下所示:

{
  "foo": "bar",
  "asdf": "qwerty"
}

但是当我在 Azure 上的 iisnode 中运行相同的代码时,我得到了这个:

{"foo":"bar","asdf":"qwerty"}

并不是说它有任何功能上的差异,后者甚至节省了一些额外的字节,但很高兴知道差异来自哪里。

代码如下:

exports.test = function(req, res){
    var result = { foo : 'bar', asdf : 'qwerty'};
    res.send(result);
}

【问题讨论】:

    标签: json node.js formatting line-breaks iisnode


    【解决方案1】:

    区别可能在于NODE_ENV 环境变量和express'default configurations

    app.defaultConfiguration = function(){
      // ...
    
      this.configure('development', function(){
        this.set('json spaces', 2);
      });
    
      // ...
    };
    

    Azure 必须为 NODE_ENV(可能是 'production')设置不同的值,以便跳过 configure() 回调。

    【讨论】:

    • 谢谢,确实是这个原因。我通过在 iisnode.yml 中设置 node_env: development 使 iisnode 中的“开发”设置正常工作。
    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    相关资源
    最近更新 更多