简体   繁体   中英

Node.js http.get as a function

I am trying to make a function that returns the content of the webpage and this is what I have so far

var get_contents = function() {
    var httpRequestParams = 
    {
        host: "google.com",
        port: 80,
        path: "/?"
    };

    var req = http.get(httpRequestParams, function(res) 
    {
        var data = '';
        res.on('data', function(chunk) {
            data += chunk.toString();
        });
        //console.log(data);
    }).end();

    return req;
}

This when I run this code, I see the html contents when the console logging is turned on but when I try to return the output, it just never works.

I can't figure out a way to return get_contents() anywhere. On the console, it just doesnt respond.

Thanks

Something like that: (dont forget to handle error and timeout)

var on_contents = function(cb) {
    var httpRequestParams = 
    {
        host: "google.com",
        port: 80,
        path: "/?"
    };

    var req = http.get(httpRequestParams, function(res) 
    {
        var data = '';
        res.on('data', function(chunk) {
            data += chunk.toString();
        });

        res.on('end', function(){
            cb(data);
        });

        //console.log(data);
    }).end();

}

function onFinish(data) {
    console.log(data);

}

on_contents(onFinish)

The short answer is: You can't return the data from that function. http.get is asynchronous, so it doesn't actually start running the callback until after your function ends. You'll need to have your get_contents function take a callback itself, check in the http.get handler whether you're done loading and, if you are, call the get_contents callback.

There is an awesome module [request][1] available in node.js.

var request = require('request'),
    url = require('url');

var server = http.createServer(function (request, response) {
    getPage("http://isohunt.com/torrents/?iht=-1&ihq=life+is+beautiful", function (body) {

        console.log(body);           
    })
});
server.listen(3000);

More information can be found on http://www.catonmat.net/blog/nodejs-modules-request/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM