Nodejs http request not working

偶尔善良 提交于 2019-12-06 11:56:04

Update

The grunt task terminated before the OP received a response; adding async and a callback to the task fixed it.


If I take your code outside of the function and prepend var http = require('http'); I get a response up until 222, at which point it dies with SyntaxError: Unexpected token <. Which is actually dying because you're trying to parse an HTML response as JSON.

If you paste the entire script below and run it end to end, the console dies with:

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)

The script:

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");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!