nodejs,对后端服务器发出https请求

时间:2014-02-25 10:37:09

标签: node.js

使用https请求,我尝试使用以下文档指南连接到后端服务器: http://nodejs.org/api/https.html#https_https_request_options_callback

    function getHttpsReq (request, response)
    {
        var options = {
            hostname: 'encrypted.google.com',
            port: 443,
            path: '/',
            rejectUnauthorized: false,
            strictSSL: false,
            agent: false,
            method: 'GET'
        }

        https.request(options, function(res) {
            console.log("statusCode: ", res.statusCode);
            console.log("headers: ", res.headers);
            var data = '';
            res.on('data', function(d) {
                process.stdout.write(d);
                data += data + d;
            });
            res.on('end', function() {
                console.log("Done");
                response.end(data);
            });
        });
    }

但是当我尝试这个时,它会抛出以下错误:

  

错误:140735290610048:错误:0607907F:数字信封   例程:EVP_PKEY_get1_RSA:期待一个rsa   键:../ DEPS / OpenSSL的/ OpenSSL的/加密/ EVP / p_lib.c:288:

  $ node --version
  v0.8.15

请让我知道我需要做些什么吗?

1 个答案:

答案 0 :(得分:0)

您需要的是您所在域的证书。

可以使用以下代码添加。

key: fs.readFileSync('/etc/apache2/ssl/domain.key'),
cert: fs.readFileSync('/etc/apache2/ssl/domain.crt')

你的选择。

可以使用受信任的颁发机构获取证书,也可以使用自签名证书。但是,如果未将自签名证书添加到客户端计算机中的受信任证书中,则自签名证书将在客户端生成错误。