Express.js- add response body

后端 未结 2 2009
醉话见心
醉话见心 2021-02-14 17:34

I would like to add a body property to Express.js\' response object, which will be called every time the send method is called, I do it by adding the f

相关标签:
2条回答
  • 2021-02-14 18:05

    You have to use res.json(body). It will send "body" as a response body.Make sure body should be object.

    0 讨论(0)
  • 2021-02-14 18:08

    You are probably using something like this:

    res.send({ foo : 'bar' });
    

    In other words, you're passing an object to res.send.

    This will do the following:

    • call res.send with the object as argument
    • res.send checks the argument type and sees that it's an object, which it passed to res.json
    • res.json converts the object to a JSON string, and calls res.send again, but this time with the JSON string as argument
    0 讨论(0)
提交回复
热议问题