node and express send json formatted node and express send json formatted express express

node and express send json formatted


try to set the "secret" property json spaces on the Node app.

app.set('json spaces', 2)

This statement above will produce indentation on json content.


You're going to have to set the Content-Type to application/json like this

app.get('/', function (req, res) {    users.find({}).toArray(function(err, results){        res.header("Content-Type",'application/json');        res.send(JSON.stringify(results, null, 4));  });});


Use type('json') to set Content-Type and JSON.stringify() for formatting:

var app = express();app.get('/', (req, res) => {  users.find({}).toArray((err, results) => {    res.type('json').send(JSON.stringify(results, null, 2) + '\n');  });});