2014-09-13 69 views
3

我试图从http上(How to create a simple http proxy in node.js?)接受的答案转换为https。的Node.js - 监听器必须是一个函数错误

当我尝试从我的浏览器访问代理服务器退出并抛出这个错误:

events.js:171 
    throw TypeError('listener must be a function'); 
    ^
TypeError: listener must be a function 

这里是我的代码:

var https = require('https'); 
var fs = require('fs'); 

var ssl = { 
    ca: fs.readFileSync("cacert.pem"), 
    key: fs.readFileSync("key.pem"), 
    cert: fs.readFileSync("cert.pem") 
}; 

https.createServer(ssl, onRequest).listen(3000, '127.0.0.1'); 

function onRequest(client_req, client_res) { 

    console.log('serve: ' + client_req.url); 

    var options = { 
    hostname: 'www.example.com', 
    port: 80, 
    path: client_req.url, 
    method: 'GET' 
    }; 

    var ssl = { 
    ca: fs.readFileSync("cacert.pem"), 
    key: fs.readFileSync("key.pem"), 
    cert: fs.readFileSync("cert.pem") 
    }; 

    var proxy = https.request(ssl, options, function(res) { 
    res.pipe(client_res, { 
     end: true 
    }); 
    }); 

    client_req.pipe(proxy, { 
    end: true 
    }); 
} 

正如你所看到的,我做了很一点变化,我不知道如何解决这个问题。

任何想法?

回答

3

看起来你已经得到了参数https.request错误(http://nodejs.org/api/https.html#https_https_request_options_callback)。应该仅仅是:

var proxy = https.request(options, function(res) { 
    res.pipe(client_res, { 
    end: true 
    }); 
}); 

您的证书信息应包括在选择对象,从链接页面:

var options = { 
    hostname: 'encrypted.google.com', 
    port: 443, 
    path: '/', 
    method: 'GET', 
    key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'), 
    cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem') 
}; 
options.agent = new https.Agent(options); 

var req = https.request(options, function(res) { 
    ... 
} 
+0

从代理中删除'ssl'当我尝试访问一个页面时抛出错误和错误:throw er; //未处理的 '错误' 事件 - 错误:34410095616:错误:140770FC:SSL例程:SSL23_GET_SERVER_HELLO:未知协议:../ DEPS/OpenSSL的/ OpenSSL的/ SSL/s23_clnt.c:787 – 2014-09-13 06:08:50

+0

这奏效了,谢谢你。 – 2014-09-14 05:09:32

1

我通过传递函数名作为帕拉姆而不是变量解决了这个错误它包含函数

相关问题