Ответ 1
Методы идентичны при передаче объекта или массива, но res.json()
также преобразует не объекты, такие как null
и undefined
, которые недействительны JSON.
Метод также использует настройки приложения json replacer
и json spaces
, поэтому вы можете форматировать JSON с дополнительными параметрами. Эти параметры заданы так:
app.set('json spaces', 2);
app.set('json replacer', replacer);
И передано в JSON.stringify()
так:
JSON.stringify(value, replacer, spacing);
// value: object to format
// replacer: rules for transforming properties encountered during stringifying
// spacing: the number of spaces for indentation
Это код в методе res.json()
, который не имеет метода отправки:
var app = this.app;
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = JSON.stringify(obj, replacer, spaces);
В конце концов метод заканчивается как res.send()
:
this.charset = this.charset || 'utf-8';
this.get('Content-Type') || this.set('Content-Type', 'application/json');
return this.send(body);