javascript - Node http.request does nothing - Stack Overflow

var http = require('http');var options = {method: 'GET',host: 'www.google'

var http = require('http');

var options = {
    method: 'GET',
    host: 'www.google',
    port: 80,
    path: '/index.html'
};

http.request(
    options,
    function(err, resBody){
        console.log("hey");
        console.log(resBody);
        if (err) {
            console.log("YOYO");
            return;
        }
    }
);

For some reason this just times out and doesn't log anything to the console.

I'm aware that I could require('request') but I need to use http to be patible with a plugin I'm using.

Also, background on my versions: Node is v0.8.2

var http = require('http');

var options = {
    method: 'GET',
    host: 'www.google.',
    port: 80,
    path: '/index.html'
};

http.request(
    options,
    function(err, resBody){
        console.log("hey");
        console.log(resBody);
        if (err) {
            console.log("YOYO");
            return;
        }
    }
);

For some reason this just times out and doesn't log anything to the console.

I'm aware that I could require('request') but I need to use http to be patible with a plugin I'm using.

Also, background on my versions: Node is v0.8.2

Share asked Feb 12, 2013 at 22:26 Ben GBen G 26.9k35 gold badges109 silver badges176 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

Use the example here: http://nodejs/api/http.html#http_http_request_options_callback

var options = {
  hostname: 'www.google.',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

the callback does not have an error parameter, you should use on("error", ...) and your request doesn't get sent till you call end()

You prepared a request object, but didn't fire it with .end(). (Also the callback doesn't work that way.)

See: http://nodejs/api/http.html#http_event_request

Couple things here:

  • Use hostname not host so you are patible with url.parse() (see here)
  • The callback for request takes one argument which is an http.ClientResponse
  • To catch an error use req.on('error', ...)
  • When using http.request you need to end the request when you are done req.end() this is so you can write any body you need to (with req.write()) before ending the request
    • Note: http.get() will do this for you under the hood, which may be why you forgot.

Working code:

var http = require('http');

var options = {
    method: 'GET',
    hostname: 'www.google.',
    port: 80,
    path: '/index.html'
};

var req = http.request(
    options,
    function(res){
        console.log("hey");
        console.log(res);
    }
);

req.on('error', function(err) {
  console.log('problem', err);
});

req.end();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744860711a4597684.html

相关推荐

  • javascript - Node http.request does nothing - Stack Overflow

    var http = require('http');var options = {method: 'GET',host: 'www.google'

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信