我想代理对新端口的调用,因此我将创建服务器的所有逻辑封装到像
这样的函数中var appRouter = express.Router();
app.use(appRouter);
appRouter.route('*')
.get(function (req, res) {
proxyRequest(req, res)
}
在代理请求函数中我放置了以下代码
function proxyRequest(req, res) {
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
var hostname = req.headers.host.split(":")[0];
proxy.web(req, res, {
target: 'http://' + hostname + ':' + 5000
});
http.createServer(function (req, res) {
console.log("App proxy new port is: " + 5000)
res.end("Request received on " + 5000);
}).listen(5000);
}
问题是,当我进行第一次调用时,我看到代理工作正常,并在新服务器端口 5000 上监听,当我第二次点击浏览器时出现错误
Error: listen EADDRINUSE
at exports._errnoException (util.js:746:11)
at Server._listen2 (net.js:1146:14)
我应该如何避免这种情况
最佳答案
从 proxyRequest
函数中删除代理服务器创建逻辑
试试这个:
httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
http.createServer(function (req, res) {
console.log("App proxy new port is: " + 5000)
res.end("Request received on " + 5000);
}).listen(5000);
function proxyRequest(req, res) {
var hostname = req.headers.host.split(":")[0];
proxy.web(req, res, {
target: 'http://' + hostname + ':' + 5000
});
}
关于javascript - 代理请求超过时间提供错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265253/