Vite插件采坑记录

197 阅读1分钟

背景

最近在 Vite 项目需要模拟远程代理接口,插件需要在configureServer钩子的server中间件,使用axios请求https://协议接口

错误信息

Error: unsupported certificate purpose

错误原因

请求https协议接口需要证书

解决方法

构造证书,再进行请求

import axios from 'axios';
import https from 'https';

axios.get(htmlUrl, {
    httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});

参考资料:axios

httpAgent and httpsAgent define a custom agent to be used when performing http and https requests, respectively, in node.js.