1. Generate a certificate locally
# 进入项目并在build下新建cert目录
cd ./your-projuct/build && mkdir cert
# 进入cert目录
cd cert
# 使用openssl生成私钥
openssl genrsa -out private.key 1024
# 使用上面生成的私钥生成证书 其中的Common Name输入后端接口的host
openssl req -new -key private.key -out csr.key
# 生成证书签名文件
openssl x509 -req -days 3650 -in csr.key -signkey private.key -out file.crt
(When the private key generated in the fourth step generates the certificate, the Common Name is configured to enter the host of the back-end interface)
2. Modify the configuration file of the local startup project
app.js or ./build/dev-server.js, here to intercept part of the changed code
const opn = require('opn');
const path = require('path');
const express = require('express');
const webpack = require('webpack');
const proxyMiddleware = require('http-proxy-middleware’);
const webpackConfig = require('./webpack.dev.conf');
const app = express();
const compiler = webpack(webpackConfig);
.
.
.
// 新增部分
const fs = require('fs');
const https = require('https’);
const privateKey = fs.readFileSync(path.join(__dirname, './cert/private.key'),'utf8');
const certificate = fs.readFileSync(path.join(__dirname, './cert/file.crt'),'utf8');
const credentials = { key: privateKey, cert: certificate };
const server = https.createServer(credentials, app);
server.listen(port);
3. Restart the project
3.1 Problem: Chrome browser cannot handle messy credentials and cannot access https localhost url
solve : click anywhere on the page, directly use the keyboard to input thisisunsafe and press Enter to access normally
3.2 Problem: Unable to access the back-end interface, error DEPTH_ZERO_SELF_SIGNED_CERT is reported
solves : need to add secure:false to each object of proxyTable
// 每个对象中增加secure:false
const proxyHost = 'https://www.your-domain.com';
const proxyTable = {
'/YOURKEYWORD': {
target: proxyHost,
changeOrigin: true,
secure: false
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。