res.json()
Sends a JSON response composed of the specified data
.
return res.json(data);
When an object or array is passed to it, this method is identical to res.send()
. Unlike res.send()
, however, res.json()
may also be used for explicit JSON conversion of non-objects (null, undefined, etc.), even though these are technically not valid JSON.
return res.json({ firstName: 'Tobi' });
return res.status(201).json({ id: 201721 });
var leena = await User.findOne({ firstName: 'Leena' });
if (!leena) { return res.notFound(); }
return res.json(leena.id);//« you can send down primitives, like numbers
- Don't forget that this method's name is all lowercase.
- This method is terminal, meaning that it is generally the last line of code your app should run for a given request (hence the advisory usage of
return
throughout these docs).