http-proxy-middleware-for-koa2
v1.1.0
Published
用于koa2的http-proxy-middleware的中间件
Downloads
2
Maintainers
Readme
http-proxy-middleware-for-koa2
http-proxy-middleware for koa 2.x.
Usage
const app = require('koa')();
const proxy = require("http-proxy-middleware-for-koa2");
const proxyOptions = {
targets: {
"/file-api": {
target: "https://www.baidu.com/",
ws: false,
secure: true, // https 需要配置
changeOrigin: true,
pathRewrite: {
"^/file-api": "/common", // 需要的rewrite
},
onProxyRes(proxyRes, req, res) {
console.log(proxyRes);
console.log(req);
console.log(res);
},
},
},
};
app.use(proxy(proxyOptions));
Example usage
// /config/proxy.config.js
const proxy = {
targets: {
"/file-api": {
target: "",
ws: false,
secure: true, // https 需要配置
changeOrigin: true,
pathRewrite: {
"^/file-api": "/common", // 需要的rewrite
},
onProxyRes(proxyRes, req, res) {
console.log(proxyRes);
console.log(req);
console.log(res);
},
},
},
};
module.exports = proxy;
// /app.js
const Koa = require("koa");
const app = new Koa();
const proxy = require("http-proxy-middleware-for-koa2");
const proxyOptions = require("./config/proxy.config");
app.use(proxy(proxyOptions));
配置项请查看
http-proxy-middleware options