expressjs如何配置自有证书https服务

自建证书,Webpack-dev-server开发模式下https直接调用会报错[Error: DEPTH_ZERO_SELF_SIGNED_CERT],需要设置一下代理配置

"proxy": {
    "/api": {
      "target": "https://localhost:8080/api/",//转向的url
      "changeOrigin": true, //解决跨域问题
      "secure": false, //接收无效证书的https服务
      logLevel: 'debug', //打印日志
      pathRewrite: {
        '^/api': ''
      }
    }
  }

安装openssl

windows需要git-bash,或者安装oepnssl,https://slproweb.com/products/Win32OpenSSL.html

用openssl生成证书

openssl中有如下后缀名的文件

.key格式:私有的密钥
.csr格式:证书签名请求(证书请求文件),含有公钥信息,certificate signing request的缩写
.crt格式:证书文件,certificate的缩写
.crl格式:证书吊销列表,Certificate Revocation List的缩写
.pem格式:用于导出,导入证书时候的证书的格式,有证书开头,结尾的格式

步骤

openssl genrsa 1024 > /pathway/private.pem
openssl req -new -key /pathway/private.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey /pathway/private.pem -out /pathway/file.crt

express中使用证书

const express = require('express');
var fs = require('fs');
var https = require('https');

https.createServer({
  key: fs.readFileSync('./certificate/private.pem'),
  cert: fs.readFileSync('./certificate/file.crt'),
  // requestCert: false,
  // rejectUnauthorized: false,
}, app).listen(process.env.PORT || 8080, () => console.log(`Listening on port ${process.env.PORT || 8080}!`));

你可能感兴趣的:(expressjs如何配置自有证书https服务)