27 EYLÜL 2013, Cuma
Res arasındaki fark.gönder ve res.json Express.js
res.send
ve de res.json
arasındaki gerçek fark nedir istemci yanıt aynı işlemi yapmak gibi görünüyor.
CEVAP
27 EYLÜL 2013, Cuma
Bu yöntem nesne ya da bir dizi geçti, ama res.json()
da olmayan nesneleri dönüştürür, null
ve JSON geçerli değildir undefined
gibi zaman zaman aynıdır.
Bu yöntem aynı zamanda json replacer
json spaces
uygulama ayarlarını kullanır, daha fazla seçenek ile JSON formatında. Bu seçenekler: gibi set
app.set('json spaces', 2);
app.set('json replacer', replacer);
Ve şu şekilde: JSON.stringify()
geçti
JSON.stringify(value, replacer, spacing);
// value: object to format
// replacer: rules for transforming properties encountered during stringifying
// spacing: the number of spaces for indentation
Bu yöntemi yok Gönderen res.json()
yönteminin kodu şöyledir:
var app = this.app;
var replacer = app.get('json replacer');
var spaces = app.get('json spaces');
var body = JSON.stringify(obj, replacer, spaces);
Bu yöntem sonunda res.send()
olarak bulur:
this.charset = this.charset || 'utf-8';
this.get('Content-Type') || this.set('Content-Type', 'application/json');
return this.send(body);
Bunu Paylaş:
Paket tilde(~) ve şapka arasındaki far...
'bağımlılıkları arasındaki fark n...
YAML ve JSON arasındaki fark nedir? Di...
Arasındaki fark <ınput type='b...
JSON arasındaki fark.stringify ve JSON...