2013-12-09 29 views
1

我有一段代码应该做一个http获取请求。程序成功退出没有错误,但我没有看到任何回应,它甚至没有进入回调函数!起初我以为这是因为http是异步的,并且最后放了一个大的循环,但是这也不起作用。有谁知道这个问题?只有第一个控制台日志sendHttpRequest和444被打印。我也尝试了http.get,但它也没有工作。Nodejs的http请求不起作用

function sendHttpRequest (url, callBack) { 
    console.log("sendHttpRequest"); 
    //constrct options 
    var options = { 
     host: 'www.google.com', 
     path: '/index.html', 
     method: 'GET', 
     headers: { 
      'Content-Type': 'application/x-www-form-urlencoded' 
     } 
    }; 

    http.get("http://www.google.com/index.html", function(res) { 
     console.log("Got response: " + res.statusCode); 
    }); 

    var req = http.request(options, function(res) { 
     console.log("333"); 
     var output = ''; 
     console.log(options.host + ':' + res.statusCode); 
     res.setEncoding('utf8'); 

     res.on('data', function (chunk) { 
      console.log("DATATATAT!") 
      output += chunk; 
     }); 

     res.on('end', function() { 
      console.log('222'); 
      var obj = JSON.parse(output); 
      callBack(res.statusCode, obj); 
     }); 
    }); 

    req.on('error', function (err) { 
     console.log('error: ' + err.message); 
    }); 

    req.end(); 
    console.log("444"); 
    } 
} 
+0

有人说谷歌没有“/指数.html“,我删除了它,但仍然无效。它只是没有进入回调。没有反应。 – user3084182

+0

你打这个电话过得怎么样?这有几个问题,但除此之外,我可以看到所有的'console.log',并且回调函数也被调用。你也返回HTML,'JSON.parse'永远不会工作。 – matth

+0

@ making3嗨,我把它封装成一个模块,并从一个咕task任务中调用它,但不知何故,我只能看到第一个控制台日志和最后一个,它是444.我必须在grunt或这个模块中调用异步等待回应?谢谢!对于json部分,原本我打算解析一个json,这样不会有问题。 – user3084182

回答

4

更新

的OP接收到响应之前终止该任务咕噜;加入async并回调固定它的任务。


如果我把你的代码的功能之外,在前面加上var http = require('http');我得到回应,直到222,此时它与SyntaxError: Unexpected token <死亡。这实际上是因为您试图将HTML响应解析为JSON而死亡。

如果粘贴下面的整个脚本并运行它端到端,控制台与死亡:

undefined:1 
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> 
^ 
SyntaxError: Unexpected token < 
    at Object.parse (native) 
    at IncomingMessage.<anonymous> (/Users/you/nodetest/tmp/test.js:31:28) 
    at IncomingMessage.EventEmitter.emit (events.js:120:20) 
    at _stream_readable.js:896:16 
    at process._tickCallback (node.js:599:11) 

脚本:

var http = require('http'); 

console.log("sendHttpRequest"); 
//constrct options 
var options = { 
    host: 'www.google.com', 
    path: '/index.html', 
    method: 'GET', 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    } 
}; 

http.get("http://www.google.com/index.html", function(res) { 
    console.log("Got response: " + res.statusCode); 
}); 

var req = http.request(options, function(res) { 
    console.log("333"); 
    var output = ''; 
    console.log(options.host + ':' + res.statusCode); 
    res.setEncoding('utf8'); 

    res.on('data', function (chunk) { 
     console.log("DATATATAT!") 
     output += chunk; 
    }); 

    res.on('end', function() { 
     console.log('222'); 
     // it's failing on the next line, because the output 
     // it's receiving from Google is HTML, not JSON. 
     // If you comment out this line and simply 
     // "console.log(output)" you'll see the HTML response. 
     var obj = JSON.parse(output); 
     callBack(res.statusCode, obj); 
    }); 
}); 

req.on('error', function (err) { 
    console.log('error: ' + err.message); 
}); 

req.end(); 
console.log("444"); 
+0

嗨,我把这个包装起来作为一个模块,并从一个咕task任务调用它,但不知何故,我只看到第一个控制台日志和最后一个,这是444.我必须调用异步咕噜或此模块等待回应?谢谢!对于json部分,原本我打算解析一个json,这样不会有问题。 – user3084182

+0

你将需要在你的问题中提供包装,以便实际确定 - 开箱即用,你可以看到这可以工作,但是如果你的咕噜任务在结束之前终止(并且正在运行async),这可能是一个不错的选择,grunt不会等待它完成。 – brandonscript

+0

原来你是对的!在得到响应之前,咕task任务终止了!我添加了异步和回调,它工作。非常感谢!! :) – user3084182