使用Node HTTP请求从GitHub API挂起套接字错误

时间:2014-11-11 15:22:50

标签: node.js http github

我正在提出以下请求:

var options = {
    host: "api.github.com",
    port: 443,
    path: "/repos/myusername/myreponame/issues",
    headers: {Authorization: "Basic " + new Buffer(username + ":" + password).toString("base64")}
};

var request = http.get(options, function(response) {
    response.on("data", function(data) {
        console.log("data");
    });
});

request.on("error", function(error) {
  console.log(error);
});

这会导致错误:

{ [Error: socket hang up] code: 'ECONNRESET' }

任何想法为什么?

1 个答案:

答案 0 :(得分:3)

我想出了这个。我使用的是http而不是https。这是工作代码:

var https = require("https");

username = "myusername";
password = "mypassword";

var options = {
    host: "api.github.com",
    port: 443,
    path: "/repos/myusername/myreponame/issues",
    headers: {
        "Authorization": "Basic " + new Buffer(username + ":" + password).toString("base64"),
        "User-Agent": username
    }
};

var request = https.get(options, function(response) {
    response.on("data", function(data) {
        process.stdout.write(data);
    });
});

request.on("error", function(error) {
  console.log(error);
});