2

我正在使用 NODE.js 我正在使用“请求”模块:

有用:

var request = require('request');
request('http://www.site.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

当我尝试包含参数时,它会崩溃!

var request = require('request');
request('http://www.site.com/index.php?action=y9sadf98yh', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

Error: undefined is not a valid uri or options object.
    at request (/node_modules/request/index.js:1217:41)
    at Request._callback (nodeCloseSession/index.js:60:5)
    at Request.self.callback (/node_modules/request/index.js:148:22)
    at Request.EventEmitter.emit (events.js:98:17)
    at Request.<anonymous> (/node_modules/request/index.js:876:14)
    at Request.EventEmitter.emit (events.js:117:20)
    at IncomingMessage.<anonymous> (/node_modules/request/index.js:827:12)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:883:14
    at process._tickCallback (node.js:415:13)

如何在 NODE.js 'request' 模块中使用参数?

4

2 回答 2

3

您可以尝试使用Requestify作为替代方案,这是一个用于 nodeJS 的小型且非常简单的 HTTP 客户端 + 它支持缓存..

要执行您的请求,请执行以下操作:

var requestify = require('requestify');

requestify.get('http://www.site.com/index.php?action=y9sadf98yh')
  .then(function(response) {
      // Get the response body (JSON parsed or jQuery object for XMLs)
      response.getBody();
  }
);

或者:

var requestify = require('requestify');

requestify.get('http://www.site.com/index.php', { 
    params: {
      action: 'y9sadf98yh'
    }
})
  .then(function(response) {
      // Get the response body (JSON parsed or jQuery object for XMLs)
      response.getBody();
  }
);
于 2013-08-03T19:27:53.460 回答
0
request('/www.site.com/', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
});

使用 userAgent 获取您的参数值

于 2014-11-19T06:56:49.927 回答