admin管理员组文章数量:1415145
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 sendsPOST
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'shttp.requst
handle redirects by default. – kirilloid Commented Sep 3, 2013 at 19:03
2 Answers
Reset to default 3When 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!
本文标签: javascriptGET Request in nodejs is not workingStack Overflow
版权声明:本文标题:javascript - GET Request in node.js is not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745203231a2647498.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论