Express 配置 https / 443 安全链接

按照教程已配置成功

 

前一部分内容参照
    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

转载于:https://www.cnblogs.com/CalmWorld/p/8933307.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值