最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - GET Request in node.js is not working - Stack Overflow

programmeradmin1浏览0评论

I'm having big troubles loading a page through Node.js. This is the code of my Node.js Application, please note it works if I try to load other resources (like www.google) so the question is specific to the domain name www.egopay:

 var http = require('http');
 var options = {
    hostname: 'www.egopay',
    port: 80,
    path: '/',
    method: 'GET'
};

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();

Using cURL it works, Node.js won't.

Around one minute after the request this error shows up:

problem with request: read ECONNRESET

I'm having big troubles loading a page through Node.js. This is the code of my Node.js Application, please note it works if I try to load other resources (like www.google.) so the question is specific to the domain name www.egopay.:

 var http = require('http');
 var options = {
    hostname: 'www.egopay.',
    port: 80,
    path: '/',
    method: 'GET'
};

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();

Using cURL it works, Node.js won't.

Around one minute after the request this error shows up:

problem with request: read ECONNRESET
Share Improve this question edited Sep 3, 2013 at 19:02 Gotenks asked Sep 3, 2013 at 18:48 GotenksGotenks 3771 gold badge6 silver badges14 bronze badges 4
  • You said GET request in title of your question, but actually sends POST in your code. Maybe, just check things? – kirilloid Commented Sep 3, 2013 at 18:52
  • Both don't work, I have changed the code to reflect the question title. – Gotenks Commented Sep 3, 2013 at 18:57
  • why are you writing a body for a GET request? technically it is allowed but is probably blocked by the server – FlavorScape Commented Sep 3, 2013 at 19:01
  • Just opening this URL in browser redirects me to https site. Maybe, your curl mand follows redirect (even though it is not so w/o additional flag). Hardly node's http.requst handle redirects by default. – kirilloid Commented Sep 3, 2013 at 19:03
Add a ment  | 

2 Answers 2

Reset to default 3

When I made the same request, but added a user-agent to the headers, I got a response, that made it clear what was happening. Try adding this line to your options:

headers: {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36'},

Basically, they have an nginx server that redirects you to an HTTPS site with the same URL. Your not getting a response without including a user agent is probably a bug in the nginx deployment for that site. I can explain it no other way, as I cannot recreate the circumstances in Chrome or Firefox. Even when overriding with an empty user-agent. I imagine it is the difference between an empty string and an undefined string. Node is sending the header's field as pletely undefined, whereas the browsers I'm trying to replicate with are only sending an empty string. Again, can't recreate, but this is my best guess.

I had similar issues with requests in Node js so i wrote this npm package:

https://npmjs/package/curljs

Hope it helps!

发布评论

评论列表(0)

  1. 暂无评论