vue2
'/api': {
target:'https://www.baidu.com/',
// secure: true, // 如果是 https ,需要开启这个选项
changeOrigin: true, // 是否跨域
onProxyRes(proxyRes, req, res) { // 增加代理的真实地址
const realUrl = new URL(req.url || '', '请求地址' || '')
proxyRes.headers['x-real-url1'] = realUrl
},
pathRewrite: {
'^/api': '/api'
}
},
vue3 vite
proxy: {
'/api': {
target: 'https://www.baidu.com/',
changeOrigin: true
rewrite: (path) => path.replace(/^\/api/, ''),
bypass(req, res, options) {
const proxyUrl = new URL(options.rewrite(req.url), (options.target) as string)?.href
res.setHeader("x-res-proxyUrl", proxyUrl);
},
},
}
通用 ,利用nodeJs启动服务模拟请求
const http = require("http");
const app = http.createServer((req, res) => {
// 查看收到的请求地址
console.log(req.url);
});
app.listen(3000, "localhost", () => {
console.log("localhost:3000开启服务");
});