【问题标题】:Handling a socket hang up error in Node.js处理 Node.js 中的套接字挂起错误
【发布时间】:2012-11-15 11:09:59
【问题描述】:

您好,当我尝试从我的 node.js 访问 URL 时,我收到以下错误。

请求问题:

套接字挂断

我的代码如下:

var express = require('express')
app = express.createServer();
var options = {
    host:'10.230.125.54',
    port:8080,
    path:'/'
};
var http = require('http');
app.get('/hitmideation', function(req, response) {
    var jsonresponse;
    response.contentType('application/json');
    console.log('listening');
    var req =http.request(options, function(res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
            jsonresponse = chunk;
            response.end('json from url:'+ JSON.stringify(jsonresponse));
        });
    });
    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });
    req.end();
});
console.log('I will be listening on: 1340' );
app.listen(1340);

我该如何处理这个问题?任何帮助将不胜感激

【问题讨论】:

    标签: node.js express


    【解决方案1】:

    通过在浏览器中进行测试,确保您在options 中的网址正常工作。如果您确定 url 正在响应,请尝试使用以下代码 sn-p 检索内容:

    http.get("10.230.125.54:8080", function(res) {
        res.setEncoding('utf8');
        var content = '';
        res.on('data', function (chunk) {
            content += chunk;
        });
        res.on('end', function () {
            var obj = JSON.parse(content);
            // do whatever with the obj
        });
    }).on('error', function(err) {
           // Handle the err here
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-07
      • 2014-03-21
      • 2014-11-08
      • 2021-01-02
      • 1970-01-01
      • 2013-09-12
      • 2015-09-13
      • 2014-10-02
      相关资源
      最近更新 更多