javascript - 使用 Node 代理时出错

标签 javascript node.js express node-http-proxy

我使用以下程序,当我运行它时出现以下错误我能够运行该应用程序但是当我放入浏览器 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/

相关文章:

javascript - Vue.js - 将新数据添加或设置到存储中的数组

javascript - 异步如何真正工作以及如何在 node.js (node-webkit) 中正确使用它

node.js - Dialogflow : Set multiple contexts using fulfillment library (node. js) 在 Dialogflow

node.js - 在 Sequelize.js 中调用存储过程

asp.net - 将 Monolith ASP.NET Webform 应用程序移动到微服务和 session 管理

angularjs - 当我进行 api 调用时,如何让数据库不附加状态?

node.js - 项目没有从数据库中删除,该项目应该是 :item in the app. 删除 ('/todo/:item' ) int 下面的代码

javascript - 在 jqGrid 内联编辑中获取选择的文本部分而不是值

javascript - 关于高阶函数回调中的 ‘this’ 的问题

javascript - 如何循环遍历数组并为 vue.js 中的所有对象添加属性