Nodejs http request not working

我们两清 提交于 2019-12-07 18:46:40

问题


I have a piece of code that's supposed to do a http get request. The program exited successfully without error, but I didn't see any response and it didn't even go inside the callback function! At first I thought it's because http is asynchronous and put a large loop in the end but that didn't work either. Does anyone know this issue? Only the first console log sendHttpRequest and 444 gets printed. I also tried the http.get but it didn't work either.

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");
    }
}

回答1:


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");


来源:https://stackoverflow.com/questions/20479218/nodejs-http-request-not-working

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