I try to proxy all interfaces of http://A.com to http://B.com. And then I want to change the result of interface '/api/v2'. But what i do is not worked for the interface '/api/v2'. My logs in proxyRes don't run. Here is the code in my server.
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use('/api/v2', createProxyMiddleware({
target: 'http://B.com',
changeOrigin: true,
on: {
proxyRes: (proxyRes, req, res) => {
console.log('proxy res here =====');
proxyRes.headers['x-added'] = 'foobar';
}
}
}));
app.use('/', createProxyMiddleware({ target: 'http://B.com', changeOrigin: true }));
// 启动服务器
const port = 3000;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Is there anything wrong with my code?
Proxy A.com to B.com is worked. But it seems app.use('/api/v2', ......)
is not worked at all.
I want to change its response header and response data.
env
- node: 14.18.3
- express: 4.18.2
- http-proxy-middleware: 2.0.6
From the http-proxy events documentation, it should be
option.onProxyRes
.Test:
The
x-added: foobar
response header is added.