http进入nodejs

var http = require('http'); exports.get = function(){ var options = { host: 'http://stackoverflow.com/' }; http.get(options, function(res) { console.log("Got response: " + res.statusCode); }).on('error', function(e) { console.log("Got error: " + e.message); }); }; 

给我错误Got error: getaddrinfo ENOTFOUND http://stackoverflow.com/ http://stackoverflow.com/:80我使用的文档; https://nodejs.org/docs/v0.5.2/api/http.html#request.url

可能是什么问题,我把端口号添加到选项中;

 var options = { host: 'http://stackoverflow.com/' port:443 //also port:8080, port:80, port: 8118 }; 

但仍然没有解决办法。 是关于公司代理吗?

我怎样才能到达链接?

主机不是URL。 尝试

stackoverflow.com为主机。

 var options = { host: 'stackoverflow.com', port: 80, path: '/', method: 'GET' }; var req = http.request(options, function(res) { console.log('STATUS: ' + res.statusCode); console.log('HEADERS: ' + JSON.stringify(res.headers)); ... 

这个问题有时会根据主机发生,像“stackoverflow.com”一些主机可能会返回200,在某些情况下,像“google.com”可能会返回301状态码,所以你应该填写主机variables像下面获得状态代码200:

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

与主机:“www.google.com”我得到200状态码。 但与主机:'stackoverflow.com'只有当我删除'www。' 我将得到状态代码200。

任何人有这个问题,你应该尝试使用和不使用“www”。 得到正确的结果。