2012-07-19 69 views
3

我幾乎下面這個教程(所有其他教程,我發現看起來是一樣的)無法在express.js安裝HTTPS

http://www.hacksparrow.com/express-js-https.html

我的代碼如下:

// dependencies 
var express = require('express') 
    , https = require('https') 
    , fs = require('fs'); 

var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString(); 
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString(); 

var app = express.createServer({ 
    key : privateKey 
, cert : certificate 
}); 

... 

// start server 
https.createServer(app).listen(app.get('port'), function(){ 
    console.log("Express server listening on port " + app.get('port')); 
}); 

的應用程序啓動sudo的節點應用程序後,細現在

Express server listening on port 443 

當我蜷縮

curl https://localhost/ 

我得到

curl: (35) Unknown SSL protocol error in connection to localhost:443 

任何想法?

+0

如果您刪除對key和cert的'toString'調用,是否會得到相同的錯誤? – 2012-07-19 19:17:43

+0

雅,我刪除toString後得到相同的錯誤 – Max 2012-07-19 19:23:39

+0

這裏簡潔地回答了這裏:http://stackoverflow.com/a/23894573/1882064 – arcseldon 2014-10-09 15:29:15

回答

3

由於Express 3.x現在正在通過npm發佈,「app()」 - 應用程序功能已更改。在https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x上有一個遷移信息。 Express 2.x SSL教程都不再適用。 Express 3.x的正確代碼是:

// dependencies 
var express = require('express') 
    , https = require('https') 
    , fs = require('fs'); 

var privateKey = fs.readFileSync('./ssl/rp-key.pem').toString(); 
var certificate = fs.readFileSync('./ssl/rp-cert.pem').toString(); 

var options = { 
    key : privateKey 
, cert : certificate 
} 
var app = express(); 

... 

// start server 
https.createServer(options,app).listen(app.get('port'), function(){ 
    console.log("Express server listening on port " + app.get('port')); 
}); 
+0

https.createServer部分是由express生成的;它默認是http.createServer(app).listen ...;讓我試試,雖然 – Max 2012-07-19 19:23:28

+0

仍然有相同的錯誤 – Max 2012-07-19 19:25:49

+1

它必須是你的鑰匙/證書然後。你如何創建它們? – Chris 2012-07-19 19:26:37