Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JSON formatting in iisnode

JSON formatting in iisnode

Problem

When running my node.js application locally, I get nicely formatted JSON output with line breaks and spaces, like this:

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

But when I run the same code in iisnode on Azure, I get this:

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

Not that it makes any functional difference, and the latter one even saves some extra bytes, but it would be nice to know where the difference comes from.

Here is the code:

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

Solution

The difference is likely with the NODE_ENV environment variable and express' default configurations:

app.defaultConfiguration = function(){
  // ...

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

  // ...
};

Azure must have a different value for NODE_ENV (probably 'production') so the configure() callback gets skipped.

Solution courtesy of: Jonathan Lonowski

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

JSON formatting in iisnode

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×