2011-02-09 59 views
8

我一直在琢磨如何使用Node.js(v0.3.8)安全地連接到HTTP服務器。我有以下代碼:在NodeJS中創建HTTPS客戶端

var http = require("http"); 
var client = http.createClient(443, host, /* secure= */ true); 
var request = client.request("GET", relativeUrl, { host: host }); 

當我運行它,我得到:

node.js:116 
     throw e; // process.nextTick error, or 'error' event on first tick 
     ^
Error: Parse Error 
    at Client.onData [as ondata] (http.js:1287:27) 
    at Client._onReadable (net.js:648:27) 
    at IOWatcher.onReadable [as callback] (net.js:156:10) 

我一直在谷歌搜索在過去的半小時解答,並在http://nodejs.org/已經閱讀文檔。我錯過了什麼?

回答

3

如果您使用node.js作爲客戶端,您應該可以簡單地將http替換爲https。

即根據以下網站 https://github.com/danwrong/Restler/

"Transparently handle SSL (just specify https in the URL)" 
+0

我會研究使用Restler,我以前沒有遇到過。在檢查源代碼時,我注意到它使用`https`模塊,這幫助我找到了我的問題的答案。謝謝! – 2011-02-09 02:27:03