2

I am making the following nodejs server

var http = require('http');
var port = 1337;
http.createServer(function(req, res) {
    var statusList = {
      "hasErrors": false,
      "list" : [
        { "id": "spec1",
          "name": "retrieve blog info"
        },
        { "id": "spec2",
          "name": "Retrieve a Blog Avatar"
        },
        { "id": "spec3",
          "name": "Retrieve Blog's Likes"
        },
        { "id": "spec4",
          "name": "Retrieve a Blog's Followers"
        }
      ],
      "totalRecords": 4
    };
    console.log(req);
    console.log(statusList);
    res.writeHead(200, { 'Content-Type': 'application/json', "Access-Control-Allow-Origin":"*" });
    res.write(JSON.stringify(statusList));
    res.end();
}).listen(port);

But this simply display statusList without any format (simply as a string without any space or enter) on the browser page. I want to see the structured formatted json in browser so that it is clearly visible.

I also don't want to install any kind of plugin on my browser.

Also note that if i remove JSON.stringify() and simply return the object statusList as res.write(statusList); then i get the error : 'first argument must be a string or buffer'

1 Answer 1

7

Try this:

JSON.stringify(statusList, 0, 4);

Instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.