gpt4 book ai didi

node.js - 如何使用 Node 使用 Express-http-proxy 进行代理以消除最后的休息

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:36 25 4
gpt4 key购买 nike

在我的本地 JavaScript 上,我想进行休息调用

/rest/speakers

并将其代理给

http://localhost:2011/rest/speakers

我的代码如下,但不能完全按照我想要的方式工作:

var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/rest', proxy('localhost:2011/'));
app.listen(8081, function () {
console.log('Listening on port 8081');
});

为了使代理工作,我实际上需要调用

/rest/rest/speakers

我有点明白了。看来我正在将我的 /rest 代理到 localhost:2011 的根目录,然后我需要深入了解该服务器的 /rest 。将 /rest 添加到 proxy(..) 末尾将被忽略。

最佳答案

尝试使用 proxyReqPathResolver 根据需要修改您的 URL。

var url = require('url'),
proxy = require('express-http-proxy');

// ... other app setup here

app.use('/rest', proxy('localhost:2011', {
proxyReqPathResolver: function(req, res) {
return '/rest/rest' + url.parse(req.url).path;
}
}));

关于node.js - 如何使用 Node 使用 Express-http-proxy 进行代理以消除最后的休息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50538296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com