添加代理文件
// index.js
const port = 9000;
const proxyTable = {
'/localProxy/url': {
target: 'http://ip:8081',
pathRewrite: {
'^/localProxy/url': '/url',
},
'changeOrigin': true,
},
};
const app = express();
//设置跨域访问
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "*");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
// proxy api requests
Object.keys(proxyTable).forEach(function (context) {
let options = proxyTable[context];
if (typeof options === 'string') {
options = {target: options};
}
app.use(proxyMiddleware(options.filter || context, options));
});
app.listen(port);
package.json
{
"name": "proxy",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node index.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"express": "^4.15.3",
"http-proxy-middleware": "^0.17.4"
}
}
安装yarn
运行npm run dev
,或者node index.js
http-proxy-middleware
配置具体使用参考:http-proxy-middleware
网友评论