无法使用express在post请求中访问查询字符串参数

时间:2016-09-08 17:11:11

标签: node.js rest express body-parser

我正在尝试通过POST请求获取查询参数,但它似乎没有收到它们。

app.use(bodyParser.urlencoded({extended:false}));

app.post('/pubs/:id/submit', function (req, res, next) {

    console.log("query: " + JSON.stringify(req.query)) //prints {}

我已经尝试通过Postman发送请求并且卷曲并且请求已正确形成。

1 个答案:

答案 0 :(得分:0)

  

使用body-parser来解析正文。

var bodyParser=require('body-parser');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

参数

的示例#1
POST http://localhost:3000/hello/id/123/access_token/1234556ccc/name/hola

console.log(JSON.stringify(req.params))

查询示例#2

POST http://localhost:3000/hello?text=abc&access_token=1234556ccc&name=hola

`console.log(JSON.stringify(req.query))`
相关问题