Res.send and res.json in Express.js

Both methods res.send and res.json are identical when an array or object is passed. Res.json will convert non-objects such as null and undefined. You can format json with more options. This options can be set as:

app.set('json

spaces', 2);

app.set('json replacer', replacer);

Then passed to JSON.stringify() like:

JSON.stringify(value,

replacer, spacing);

// value: object to format

// replacer: rules for transforming properties encountered during stringifying

// spacing: the number of spaces for indentation

The given below code in res.json() method is not in res.send method.

var app = this.app;

var replacer = app.get('json replacer');

var spaces = app.get('json spaces');

var body = JSON.stringify(obj, replacer, spaces);

It eventually calls res.send()

this.charset = this.charset || 'utf-8';

this.get('Content-Type') || this.set('Content-Type', 'application/json');

return this.send(body);