如何为express.js服务器设置SSL证书?

之前,在一个旧版本的快递中,我可以这样做:

express.createServer({key:'keyFile', cert:'certFile'}); 

但是,在更新的版本中,这不再起作用:

 var app = express(); 

我应该调用app.use()来设置证书吗? 如果这样怎么样?

请参阅Express文档以及https.createServer的Node文档 (这是特别推荐使用的):

 var privateKey = fs.readFileSync( 'privatekey.pem' ); var certificate = fs.readFileSync( 'certificate.pem' ); https.createServer({ key: privateKey, cert: certificate }, app).listen(port); 

createServer的其他选项位于: http : //nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener

我能够通过以下样板代码获得SSL的使用:

 var fs = require('fs'), http = require('http'), https = require('https'), express = require('express'); var port = 8000; var options = { key: fs.readFileSync('./ssl/privatekey.pem'), cert: fs.readFileSync('./ssl/certificate.pem'), }; var app = express(); var server = https.createServer(options, app).listen(port, function(){ console.log("Express server listening on port " + port); }); app.get('/', function (req, res) { res.writeHead(200); res.end("hello world\n"); });