最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Node.js

旗下网站admin27浏览0评论

Node.js

Node.js

Node.js - listener必须是函数错误(Node.js - listener must be a function error)

我正在尝试将接受的答案( 如何在node.js中创建一个简单的http代理? )从http转换为https。

当我尝试从浏览器访问代理时,服务器退出并抛出此错误:

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 });}

如你所见,我做了很少的改动,我不知道如何解决这个问题。

有任何想法吗?

I'm trying to convert the accepted answer on (How to create a simple http proxy in node.js?) from http to https.

When I try to access the proxy from my browser, the server quits and throws this error :

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

Here is my code :

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 });}

As you can see, I made very little changes and I'm not sure how to fix this.

Any ideas?

最满意答案

看起来你有错误的https.request参数( .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) { ...}

Looks like you've got the arguments to https.request wrong (.html#https_https_request_options_callback). Should just be:

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

Your certificate information should be included in the options object, from the linked page:

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)

  1. 暂无评论