从Request模块中的响应回调中访问请求参数

时间:2015-09-23 04:49:56

标签: node.js request

使用Request模块时,我们如何从响应回调访问请求参数?

例如,可以通过循环列表传递以下“dog”(等等)值:

var u   = require('util');
var url = "http://example.com/animals/%s";

request.get({uri: u.format(url, "dog")}, function (error, response, body) {
    if (!error && response.statusCode == 200) {

       //how could we access the value 'dog' here?
       //something like this: console.log(uri.params.animal); 
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用js的闭包属性。

var u   = require('util');
var url = "http://example.com/animals/%s";

var param = "dog";
request.get({uri: u.format(url, param )}, function (error, response, body) {
    if (!error && response.statusCode == 200) {
       //how could we access the value 'dog' here?
       console.log(param);
    }
}