按照教程已配置成功
前一部分内容参照
https://blog.csdn.net/chenyufeng1991/article/details/60340006
前半部分是生成证书文件,关键部分摘抄如下:
使用 Linux 生成如下三个文件,Linux 下自带这个工具,Windows下找这个工具相当辛苦
openssl genrsa 1024 > private.pem
openssl req -new -key private.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey private.pem -out file.crt
生成完文件后,统一放到一个 certificate 文件夹内, 注意和 Express 的 app.js 在同一个层级内,
之后,在 Express 的 www 文件内新增以下内容:
var fs = require('fs');
var https = require('https');
var options = {
key : fs.readFileSync('./certificate/private.pem', 'utf8'),
cert : fs.readFileSync('./certificate/file.crt', 'utf8')
};
var SSLPORT = 443;
var httpsServer = https.createServer(options, app);
httpsServer.listen(SSLPORT);
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
配置成功,完整的 www 文件如下:
#!/usr/bin/env node
/** * Module dependencies. */
var app = require('../app'); var debug = require('debug')('ltdweb:server'); var http = require('http'); var fs = require('fs');
//增加 https 支持 var https = require('https');
var STORAGE = require('../Storage'); var DEFINED = require('../DEFINED'); var CACHEVIEWS = require('../CacheViews');
STORAGE.InitUsers(); CACHEVIEWS.Initialize();
//同步读取密钥和签名证书 var options = { key : fs.readFileSync('./certificate/private.pem', 'utf8'), cert : fs.readFileSync('./certificate/file.crt', 'utf8') };
/** * Get port from environment and store in Express. */
var port = normalizePort(process.env.PORT || '80'); app.set('port', port);
var SSLPORT = 443;
/** * Create HTTP server. */
var server = http.createServer(app); var httpsServer = https.createServer(options, app);
/** * Listen on provided port, on all network interfaces. */
server.listen(port); server.on('error', onError); server.on('listening', onListening);
httpsServer.listen(SSLPORT); httpsServer.on('error', onError); httpsServer.on('listening', onListening);
/** * Normalize a port into a number, string, or false. */
function normalizePort(val) { var port = parseInt(val, 10);
if (isNaN(port)) { // named pipe return val; }
if (port >= 0) { // port number return port; }
return false; }
/** * Event listener for HTTP server "error" event. */
function onError(error) { if (error.syscall !== 'listen') { throw error; }
var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + 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; } }
/** * Event listener for HTTP server "listening" event. */
function onListening() { var addr = server.address(); var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; debug('Listening on ' + bind); }
openssl req -new -key /pathway/private.pem -out csr.pemopenssl req -new -key private.pem -out csr.pem
111
openssl x509 -req -days 365 -in csr.pem -signkey private.pem -out file.cr
openssl req -new -key /pathway/private.pem -out csr.pemopenssl req -new -key private.pem -out csr.pem
111
openssl x509 -req -days 365 -in csr.pem -signkey private.pem -out file.crt