配置https接口

如果面试官问http和https的区别 你不想扯理论 那就把https的配置过程说一遍 把文件格式说出来就可以了(gen,csr) 用实际操作 碾压八股文

1 安装 openSSL

openssl下载地址 https://oomake.com/download/openssl
 网盘下载地址
  https://share.weiyun.com/SVePwRs1

2:配置环境变量 找到安装目录

C:\Program Files\OpenSSL-Win64\bin

配置https接口_第1张图片

3:新建一个key文件夹 用cd 进入

   cd   C:\Users\leson\Desktop\key

4:运行

openssl genrsa -out privatekey.pem 1024

在文件夹下面会生成一个 privatekey.pem文件

5: 继续执行                

openssl req -new -key privatekey.pem -out certrequest.csr

配置https接口_第2张图片

取名字为cn 后面的全部enter 跳过

key目录中会生成 certrequest.csr 文件

6:继续运行

openssl x509 -req -in certrequest.csr -signkey privatekey.pem -out certificate.pem

 文件生成完毕

配置https接口_第3张图片

7:创建一个node后端项目 把可以文件夹移动到根目录

配置https接口_第4张图片

8:需改bin下面 www的代码 找到http的地方 同步引入 https

配置https接口_第5张图片

9: 读取文件生成做好效验

let fs  = require("fs");
var debug = require('debug')('server:server');
var http = require('http');
var  https  = require("https");

let  privateKey = fs.readFileSync("../key/privatekey.pem",'utf8');
var certificate = fs.readFileSync("../key/certificate.pem","utf8");
var  credentials = {key:privateKey,cert:certificate};

 10:生成 https的服务

var server = http.createServer(app);
var httpServer = https.createServer(credentials ,app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
httpServer.listen(443);

回到项目目录启动服务 npm start

可以通过 https://本机ip:443访问 https的默认端口为443

http访问还是和之前一样 不受影响

配置https接口_第6张图片

完整的bin/www代码如下

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
let fs  = require("fs");
var debug = require('debug')('server:server');
var http = require('http');
var  https  = require("https");

let  privateKey = fs.readFileSync("./key/privatekey.pem",{encoding:"utf-8"});
var certificate = fs.readFileSync("./key/certificate.pem",{encoding:"utf-8"});
var  credentials = {key:privateKey,cert:certificate};


/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);
var httpServer = https.createServer(credentials,app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
httpServer.listen(443);
server.on('error', onError);
server.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);
}

你可能感兴趣的:(node,node.js,https)