安装
npm install --save-dev http-proxy-middleware
使用
const express = require('express');
const proxy = require('http-proxy-middleware');
const app = express();
//对地址为/api的请求全部转发
app.use('/api', proxy({
target: 'http://localhost:8080', // 目标主机
changeOrigin: true, // 需要虚拟主机站点
ws: true, // 是否代理websockets
pathRewrite: {
'^/api' : '', // 重写请求
},
}));
// 对地址为/abc的重写目标服务器
app.use('/abc', proxy({
router: {
// 如果请求主机为'localhost:3000', 重写目标服务器为 'http://localhost:8000'
'localhost:3000' : 'http://localhost:8000'
}
}));
使用参考资料:https://www.cnblogs.com/zhaoweikai/p/9969282.html
本文介绍了如何安装并配置http-proxy-middleware中间件,通过示例展示了如何将特定路径的请求转发到不同的后端服务器,并提供了重写路径及支持WebSocket等功能的具体实现。
2799

被折叠的 条评论
为什么被折叠?



