Proper way to set response status and JSON content in a REST API made with nodejs and express

前端 未结 12 746
无人及你
无人及你 2020-11-30 20:25

I am playing around with Nodejs and express by building a small rest API. My question is, what is the good practice/best way to set the code status, as well as the response

12条回答
  •  爱一瞬间的悲伤
    2020-11-30 20:51

    try {
      var data = {foo: "bar"};
      res.json(JSON.stringify(data));
    }
    catch (e) {
      res.status(500).json(JSON.stringify(e));
    }
    

提交回复
热议问题