Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node实现反向代理 #33

Open
Wscats opened this issue Mar 6, 2018 · 0 comments
Open

node实现反向代理 #33

Wscats opened this issue Mar 6, 2018 · 0 comments

Comments

@Wscats
Copy link
Owner

Wscats commented Mar 6, 2018

安装

安装http-proxy-middleware模块

npm install http-proxy-middleware --save-dev

配置

当你请求localhost:1314/api的时候,会跳转到localhost:5000/api,由于配置了pathRewrite,则会处理连接跳到localhost:5000/

//反向代理
var proxy = require('http-proxy-middleware');
var options = {
	target: 'http://localhost:5000/', // 目标主机
	changeOrigin: true, // 需要虚拟主机站点
	pathRewrite: {
		'^/api/old-path': '/api/new-path', // rewrite path 
		'^/api': '/' // remove base path 把api的地址改为/
	},
};
var exampleProxy = proxy('/api', options); //开启代理功能,并加载配置
app.use('/api', exampleProxy); //对地址为/的请求全部转发
app.listen(1314)

参考文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant