2014-09-13 66 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

我通過傳遞函數名作爲帕拉姆而不是變量解決了這個錯誤它包含函數

相關問題