node.js 配置https
<p>node.js的https配置;
const express = require('express');
const app = express();
const https = require('https');
const fs = require('fs');
const port = 12130;</p>
<p>//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'</p>
<p>const options = {
// 方法一:使用打包后的证书文件+密码方式,不推荐
/<em>
pfx: fs.readFileSync('214157540390598.pfx'),
passphrase: '214157540390598'
</em>/</p>
<pre><code>// 方法二:使用key文件+证书文件方式,推荐
key: fs.readFileSync('214157540390598.key'),
cert: fs.readFileSync('214157540390598.pem')</code></pre>
<p>};</p>
<p>app.post("/connect", function(req, res) {
console.log("connecting");
res.send({"data": "successful!"});
});</p>
<p>//这里只需把原来的httpServer换成httpsServer就行了,可以使用和httpServer不同的端口号
var httpsServer = https.createServer(options, app);
httpsServer.listen(port, function() {
hostname = process.env.DEPLOY_HOST;
if (!hostname) {
hostname = "localhost";
}
console.log("All ready");
});</p>