基于中间件http-proxy-middleware
主要得一个坑是,1.0以上版本,引用方式改变,网上大部分教程都是1.0以下得
// include dependencies
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
// proxy middleware options
const options = {
target: 'http://www.example.org', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
'^/tileset': '', // rewrite path
},
router: {
// when request.headers.host == 'dev.localhost:3000',
// override target 'http://www.example.org' to 'http://localhost:8000'
'localhost:3001': 'http://localhost:3000'
}
};
// create the proxy (without context)
const exampleProxy = createProxyMiddleware(options);
// mount `exampleProxy` in web server
const app = express();
app.use(express.static('public'));
app.use('/tileset', exampleProxy);
app.listen(3001);