打印从快速中间件获取参数

时间:2014-07-30 09:27:45

标签: node.js express

Node.js express中间件正在parse.com上运行。

对于以下请求:http://gopaces.com/echo/test123?getParam1=AAA由app.js中的以下代码处理:

app.get('/echo/:reqParam', function(req, res) {
    console.log("reqParam " + req.params.reqParam);
    console.log("getParam1 " + req.params.getParam1);
});

打印以下内容

E2014-07-30T09:20:41.511Z]v92 Ran custom endpoint with:
  Input:{"method":"GET","url":"/echo/test123?getParam1=AAA","headers":{"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","accept-encoding":"gzip, deflate","accept-language":"en-us","host":"gopaces.com","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/538.46 (KHTML, like Gecko) Version/8.0 Safari/538.46","version":"HTTP/1.1","x-forwarded-proto":"http"}}
  Result: success/error was not called
I2014-07-30T09:20:41.617Z]reqParam test123
I2014-07-30T09:20:41.831Z]getParam1 undefined

我希望看到的地方

I2014-07-30T09:20:41.617Z]reqParam test123
I2014-07-30T09:20:41.831Z]getParam1 AAA

我做错了什么?

3 个答案:

答案 0 :(得分:0)

console.log("getParam1 " + req.query.getParam1);

http://expressjs.com/3x/api.html#req.query

答案 1 :(得分:0)

您的命名参数与URL的路径有关,而与查询字符串变量无关。您的查询字符串变量将无法在params对象中访问,而是在req.query对象中访问。

答案 2 :(得分:0)

参考完整性

对于给定的请求

curl -X PUT -d 'viaBody=bodyValue' 'http://gopaces.com/echo/parameterValue?viaQuery=queryValue'

此代码

app.put('/echo/:viaParameter', function(req, res) {
    console.log("viaParameter " + req.params.viaParameter);
    console.log("viaQuery " + req.query.viaQuery);
    console.log("viaBody " + req.body.viaBody);
});

将转储值

I2014-07-30T11:21:39.336Z]viaQuery queryValue
I2014-07-30T11:21:39.337Z]viaBody bodyValue
I2014-07-30T11:21:39.338Z]viaParameter parameterValue