How to use req.body via get request in nodejs

血红的双手。 提交于 2020-06-29 02:42:11

问题


I have a form which uses a GET method. i also have an input with the name 'a'. when i handle the request on the server side (nodejs) i want to be able to use req.body.a (in order to search 'a' in the db). the problem is that the 'req.body' only seems to work with a POST method.

How can i solve this?


回答1:


If you are using GET method then the data is sent as query parameters

req.query

By the way there will be no body for GET method. If you want to send data through body use POST or PUT method.




回答2:


You can access req.body in GET method just as you would in a POST method. Here's an example:

export const getFile = (req, res) => {
  const { fileId } = req.body; 

  console.log(fileId)
}

Although you CAN do this, I would suggest avoiding it because it goes against HTTP conventions. Instead, put whatever data you want in the URL parameters, which you can access in Node.JS using req.params.



来源:https://stackoverflow.com/questions/32248711/how-to-use-req-body-via-get-request-in-nodejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!