gpt4 book ai didi

javascript - 使用 Node 代理时出错

转载 作者:行者123 更新时间:2023-11-30 12:21:20 25 4
gpt4 key购买 nike

我使用以下程序,当我运行它时出现以下错误我能够运行该应用程序但是当我放入浏览器 localhost:3000 时我在控制台中收到此错误...

**Error: connect ECONNREFUSED**
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19)

这是程序我有一个非常简单的 Node 应用程序,只有一个文件包含以下代码(这是我的服务器/app/.js

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

proxy = httpProxy.createProxyServer({});

http.createServer(function (req, res) {



switch (hostname) {
case 'localhost':
proxy.web(req, res, {target: 'http://localhost:9001'});
break;

}
}).listen(3005, function () {
console.log('original proxy listening on port: ' + 3005);
});


http.createServer(function(req, res) {
res.end("Request received on 9001");
}).listen(9056);

我想在用户点击某个 URL 时启动新的代理服务器

我为此使用了这个模块,我做错了什么?

https://github.com/nodejitsu/node-http-proxy

另一件事...当我使用这段代码时出现错误...

process.on('uncaughtException', function (err) {
console.log(err);
});

这是现在的错误,知道吗?

{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
{ [Error: socket hang up] code: 'ECONNRESET' }

最佳答案

http.createServer(function(req, res) {
res.end("Request received on 9001");
}).listen(9056);

您的 HTTP 服务器正在监听端口 9056。代理尝试连接到错误端口上的 HTTP 服务器,并在无法建立连接时抛出错误。为避免将来出现此类错误,请将端口放入变量中:

var PORT = 9001;
http.createServer(function(req, res) {
res.end("Request received on " + PORT);
}).listen(PORT);

关于javascript - 使用 Node 代理时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951716/

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