0%

node配置反向代理

安装依赖项

1
2
cnpm install express
cnpm install http-proxy-middleware

新建proxy.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var express = require('express');
var app = express();
var proxy = require('http-proxy-middleware');

var port = '3000';
var proxyTarget = 'https://www.xxx.com';

app.use('/', function (request, response) {
response.send('Hello World');
});

app.use('/api/*', proxy({
target: proxyTarget,
changeOrigin: true,
ws: true,
pathRewrite: {
'^/api': ''
}
}));

app.listen(port, function () {
console.log("server is listen at http://localhost:%s", port);
});

启动服务

1
node proxy.js