如何使用express配合代理转发请求

如何使⽤express配合代理转发请求
const express = require("express");
const app = express();
app.use("/abc/11", (req, res) => {
res.json(11);
});
app.listen(9007);
现在我们来通过类似 postman 的⼯具测试⼀下这个接⼝:
> http -b :9007/abc/11
11
可以看到服务器正常返回了 11 这个响应内容。
题外话:什么是正向代理反向代理?
解:直接连代理服务就是正向代理,否则就是反向代理。
ps: 这可能是最精简的解释了。
const express = require("express");
const proxy = require("http-proxy-middleware").createProxyMiddleware;
国家税务总局公告2011年第25号const app = express();
app.use("/", proxy(``));
app.listen(9007);
再来请求⼀下,看到其实是连接了⼀个我们根本就没有写的接⼝。它是/ip返回的内容。
> http -b :9007/ip
{
"origin": "88.76.56.14"
}
假设我需要也能代理,也需要能返回⾃⼰的内容。
const express = require("express");
const proxy = require("http-proxy-middleware").createProxyMiddleware;
const app = express();
app.use("/", proxy(``));
app.use("/abc/11", (req, res) => {
res.json(11);
});
app.listen(9007);
> http -b :9007/abc/11
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p> */
请求/abc/11得到了⼀个404 Not Found这是为什么?
这说明请求并没有⾛我们⾃⼰写的接⼝,还是被转发了。但转发的⽬标服务器上并没有这个接⼝,所以报 404 了。
为了更清晰⼀点的表⽰,我们重新写段代码:
app.use("/", proxy(`/abc`, { target: `/anything/` }));
app.use("/abc/11", (req, res) => {
res.json(11);
});
耐万古霉素肠球菌
app.listen(9007);
> http -b :9007/abc
{
"origin": "88.76.56.14",
}
> http -b :9007/abc/11
{
"origin": "88.76.56.14",
}
*/
可以看到,其实都去了/anything/。
wildcard path matching
For fine-grained control you can use wildcard matching. Glob pattern matching is done by micromatch. Visit or for more globbing examples.
createProxyMiddleware('**', {...}) matches any path, all requests will be proxied.
createProxyMiddleware('**/*.html', {...}) matches any path which ends with .html
createProxyMiddleware('/*.html', {...}) matches paths directly under path-absolute
createProxyMiddleware('/api/**/*.html', {...}) matches requests ending with .html in the path of /api
createProxyMiddleware(['/api/**', '/ajax/**'], {...}) combine multiple patterns
createProxyMiddleware(['/api/**', '!**/bad.json'], {...}) exclusion
可以发现使⽤!前缀进⾏排除,并且路径⽀持数组形式。
尝试⼀下:
const express = require("express");
const proxy = require("http-proxy-middleware").createProxyMiddleware;
const app = express();
app.use(
"/",
proxy([`/abc`, `!/abc/11`], { target: `/anything/` })
);
app.use("/abc/11", (req, res) => {
res.json(11);
});
app.listen(9007);
果然,试试就差点逝世。
[HPM] Proxy created: /abc,!/abc/11  -> /anything/
(node:13660) UnhandledPromiseRejectionWarning: Error: [HPM] Invalid context. Expecting something like: ["/api", "/ajax"] or ["/api/**", "!**.html"]
第五次全国金融工作会议着重看了⼀下它的错误,发现第⼀个/api和第⼆个/api/**有些特别。
然后我也不知道是啥意思。
那就继续试试:
app.use(
"/",
proxy([`/abc/**`, `!/abc/11`], { target: `/anything/` })
);
app.use("/abc/11", (req, res) => {
res.json(11);
});
app.listen(9007);
王磊晓芬小说免费阅读
> http -b :9007/abc
{
"origin": "88.76.56.14",
}
> http -b :9007/abc/11
上海公共网11
> http -b :9007/abc/12
{
"origin": "88.76.56.14",
}
嗯看起来现在没⽑病了,除了/abc/11返回了我们⾃⼰的内容,其他都是服务器返回的内容。
上⾯的代码我们设置了 pathRewrite 参数为 host,以及另⼀个 pathRewrite 参数。
const express = require('express')
const proxy = require('http-proxy-middleware').createProxyMiddleware
const app = express()
app.use('/', proxy(
[`/abc/**`, `!/abc/11`],
{
target: "",
pathRewrite: {
"^/abc": "/anything/",
},
},
)
);
app.use('/abc/11', (req, res) => {
res.json(11)
});
app.listen(9007);
发送以下请求,结果是什么样的呢?
> http -b :9007/abc
> http -b :9007/abc/11
> http -b :9007/abc/12
参考答案:
> http -b :9007/abc
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p> > http -b :9007/abc/11
11
汝州市> http -b :9007/abc/12
{
"origin": "88.76.56.14",
}
提⽰:
有没有想起 webpack devServer 中的 proxy?

本文发布于:2024-09-20 17:40:08,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/164642.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:代理   请求   返回   试试   精简   代码   内容
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议