无法从NodeJS服务器(Redis pmessage事件内)调用Rails端点



节点版本: 0.10.33

我可以成功地使用CURL从CLI发出POST请求并获得201响应,也可以在Chrome中使用Postman获得成功的201响应,但当我尝试使用http模块或request第三方库从NodeJS发出POST请求时,我从Node收到以下错误消息:

error:  Error: connect ECONNREFUSED
  at errnoException (net.js:904:11)
  at Object.afterConnect [as oncomplete] (net.js:895:19)

而且我根本没有从Rails获得任何日志(就好像Node出于某种原因没有找到Rails一样)。

我尝试过的NodeJS代码:(注意createPostData返回一个JSON可序列化对象,该对象将成为post主体)

在所有示例中,我确保railsHost为"localhost",railsPort为3000。

第一次尝试:

redisClient.on('pmessage', function(pattern, channel, message) {
    var postData = createPostData(channel),
        options = {
            uri: 'http://' + config.railsHost + ':' + config.railsPort + '/resource',
            method: 'POST',
            json: postData
        };
    if (postData === null) {
        return;
    }
    winston.info('Creating request using options ' + JSON.stringify(options) + 'n and POST data ' + JSON.stringify(postData));
    request(options, function (err, res, body) {
        if (err) {
            winston.error(err);
            return;
        }
        var status = res ? res.statusCode : null;
        if (status === 201) {
            winston.info('Notification processed successfully by Rails');
        } else {
            winston.error('Rails could not create the Notification');
            winston.error('HTTP Status: ' + status + ' -> ' + (res ? res.statusMessage : 'response object was null'));
        }
    });
});
winston.log('subscribing to */*/queue1/pubsub');
redisClient.psubscribe('*/*/queue1/pubsub');
winston.log('log.txt', 'subscribing to */*/queue2/pubsub');
redisClient.psubscribe('*/*/queue2/pubsub');

第二次尝试

redisClient.on('pmessage', function(pattern, channel, message) {
    var postData = createPostData(channel),
        options = {
            uri: 'http://' + config.railsHost + ':' + config.railsPort + '/resource',
            method: 'POST',
            json: true,
            body: postData
        };
// ... the rest is the same as the First Attempt

第三次尝试

redisClient.on('pmessage', function(pattern, channel, message) {
    var postData = createPostData(channel),
        options = {
            uri: 'http://' + config.railsHost + ':' + config.railsPort + '/resource',
            method: 'POST',
            // I also tried replacing 'body' below with 'formData' and 'postData'
            body: JSON.stringify(postData),
            headers: {
                'content-type': 'application/json'
                // I also messed around with upper-casing the 'c' and 't' in 'content-type'
            }
        };
// ... the rest is the same as the First Attempt

第四次尝试-现在使用NodeJS本地提供的HTTP模块

redisClient.on('pmessage', function(pattern, channel, message) {
    var postData = JSON.stringify(createPostData(channel)),
        options = {
            host: config.railsHost,
            port: config.railsPort,
            path: '/resource',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        };
    if (postData === null) {
        return;
    }
    winston.info('Creating request using options ' + JSON.stringify(options) + 'n and POST data ' + postData);
    var req = http.request(options, function (res) {
        var status = res ? res.statusCode : null;
        if (status === 201) {
            winston.info('Notification processed successfully by Rails');
        } else {
            winston.error('Rails could not create the Notification');
            winston.error('HTTP Status: ' + status + ' -> ' + (res ? res.statusMessage : 'response object was null'));
        }
    });
    req.on('error', function (err) {
        winston.error(err);
    }
    req.write(postData)
    req.end();
});
winston.log('subscribing to */*/queue1/pubsub');
redisClient.psubscribe('*/*/queue1/pubsub');
winston.log('log.txt', 'subscribing to */*/queue2/pubsub');
redisClient.psubscribe('*/*/queue2/pubsub');

有效的CURL命令

test.json文件包含与winston日志所说的我通过从终端粘贴的NodeJS请求副本发送的json相同的json。

curl -d @test.json --header "Content-Type: application/json" http://localhost:3000/resource

得到201的回复。

有效的邮差请求

与上面的CURL命令中的JSON相同——我只是从上面示例中的代码打印的终端复制粘贴Winston打印输出。

网址:http://localhost:3000/resource请求类型:POSTHeaders:key=Content-Type value=application/json对于正文,选择"raw"one_answers"JSON",然后粘贴到我从终端日志语句中复制的JSON中。

得到了201的回复。

事实证明,我使用的一个应用程序已将localhost映射到::1(IPv6),因此Rails在启动时更喜欢这样,因此在127.0.0.1上不可用。看来Node正在将localhost转换为127.0.0.1,因此连接被拒绝了错误。

使用-b选项在127.0.0.1上运行Rails解决了我的问题。我将依靠谷歌来帮助我修复我机器上的localhost k可能会涉及到对主机文件的一些琐碎更改)

最新更新