nodejs express 服务代理


//方法1
var proxy = require('express-http-proxy');
app.use('/map', proxy('https://test.baidu.com/', { proxyReqPathResolver: function (req, res) { console.log('tag', req.originalUrl) return `https://test.baidu.com/locations/${req.originalUrl}`;  } }));
//方法2
const {
  createProxyMiddleware,
  Filter,
  Options,
  RequestHandler
} = require('http-proxy-middleware');
app.use('/map', createProxyMiddleware({
  target: 'https://test.baidu.com/locations',
  changeOrigin: true
}));

 

浏览器输入:http://127.0.0.1:7004/map/邯山区,

转发代理目标(目标路由restfulapi : /map/:locathion):

https://test.baidu.com/locations/map/邯山区
posted @ 2021-03-13 15:49  江山一族  阅读(261)  评论(0编辑  收藏  举报