I was making a server using ExpressJS and this thing is really bugging me. What is the use of using .json() instead of .send() with my responses. According to Express, .send() automatically coverts the JavaScript objects into JSON strings and we do not need to use stringify.Then why use .json() with my responses.
app.get('/profile/:id', (req, res) => {
const {id} = req.params;
let found = false;
database.users.forEach(user => {
if(user.id === id) {
found = true;
return res.json(user);
}
})
if (found === false)
{
res.status(400).json('not found');
}
})
Here is the code where res.json() apppears.