nodejs 反向代理 http-proxy-middleware


const express = require('express');
const app = express();
const proxy = require('http-proxy-middleware');
const port = 3000
const bodyParser = require('body-parser');
app.use(require('cors')())
app.use(bodyParser.urlencoded({
    extended: false
}));
app.use(bodyParser.json())

var exampleProxy = proxy.createProxyMiddleware({
    target: 'http://127.0.0.1:3001',
    changeOrigin: true
})
app.use('/api', exampleProxy)
app.listen(port, () => {
    console.log('sever run to http://127.0.0.1:' + port);
})

http://127.0.0.1:3000/api -> http://127.0.0.1:3001/api

详细教程: https://www.jianshu.com/p/a248b146c55a

0.xx版本 用的是proxy. 其他版本 proxy.createProxyMiddleware