express 配置https

weixin_33924312發表於2017-06-30

首先在阿里雲中配置好雲盾證書
privatekey.pem是私有鑰匙
certificate.pem是公有鑰匙

var fs=requires('fs');  
var https=requires('https');  
var app=express();  
var privatekey = fs.readFileSync('privatekey.pem', 'utf8');  
var certificate = fs.readFileSync('certificate.pem', 'utf8');  
var options={key:privatekey, cert:certificate};  
var server = https.createServer(options, app);  
server.listen(443);

測試

app.get("/", function (req, res){  
      if (req.protocol == 'https'){  
            res.send('https');  
      }  
});

https://segmentfault.com/a/1190000008398220

相關文章