1、根目录下copy证书文件
2、编写bin目录下www文件
3、导入相关依赖模块
var app = require('../app'); //入口文件模块
var https = require('https'); //https服务
var fs = require('fs'); //fs 模块可用于与文件系统进行交互
var path = require('path');//用于处理文件路径的小工具
var debug = require('debug')('demo:httpsServer'); //开源日志工具。
4、部署https服务
//读取证书,保存为https所需的options
var privateCrt = fs.readFileSync(path.join(process.cwd(), 'https/1_azyn.ltd_bundle.crt'), 'utf8');
var privateKey = fs.readFileSync(path.join(process.cwd(), 'https/2_azyn.ltd.key'), 'utf8');
const HTTPS_OPTOIN = {
key: privateKey,
cert: privateCrt
};
//定义端口,开启服务
const SSL_PORT = 30001;
const httpsServer = https.createServer(HTTPS_OPTOIN, app);
httpsServer.listen(SSL_PORT, () => {
console.log(`HTTPS Server is running on: https://localhost:${SSL_PORT}`);
});
//监听服务流程
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof SSL_PORT === 'string'
? 'Pipe ' + SSL_PORT
: 'Port ' + SSL_PORT;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = httpsServer.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
通过以上步骤操作,可完成https的部署,我并为系统学习node及http部署相关知识,只是在自学过程中,将面对的问题解决后,分享于此并加深记忆。