7

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

2AEF2C7C-0AF4-4443-B4EE-FD8E05D33980.png
(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

00DDDE7A-5944-4C56-B6F1-1D6BBEE928A9.png

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

2C241441-9C69-4B38-ABB9-CB3F5987E57F.png

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
}

Layla
56 声望5 粉丝

Life is tough.