uniapp 配置本地 https 开发环境(基于 Vue2 的 uniapp)
1、生成本地 HTTPS 证书(mac)打开终端,运行以下命令:
mkdir ~/ssl-cert && cd ~/ssl-cert
2、生成私钥:
openssl genrsa -out localhost.key 2048
3、生成自签名证书(有效期365天):
openssl req -new -x509 -key localhost.key -out localhost.crt -days 365 -subj "/CN=localhost"
项目目录里面会生成两个文件,分别是:
server.key
server.crt
4、配置 vue.config.js(用于 uniapp H5 模式)
在根目录下找到或者创建 vue.config.js,host:localhost(本地),host:‘0.0.0.0’(你的 ip)添加如下:
const fs = require('fs');
const path = require('path');module.exports = {devServer: {https: {key: fs.readFileSync(path.resolve(__dirname, 'server.key')),cert: fs.readFileSync(path.resolve(__dirname, 'server.crt'))},host: '0.0.0.0',port: 8080,open: true}
};