Search code examples
javascriptnode.jsget

Accessing the data from a Node JS GET request without chaining callback functions


I'm using node.js simply so that I can run scheduled tasks and use GET requests. I'll paste some code that displays what I want to do, although it doesn't work for an obvious reason:

const http = require("http");
const request = require("request");

http.createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/html"});
    res.write("Hello, World!");
    let a = getRequest();
    console.log(a);
    res.end();
}).listen(8080);

function getRequest() {
    let b;
    request("http://www.google.com", function(err, res, body) {
        b = body;
    })
    return b;
}

So the b from the body does not work due to how the request is asynchronous and this leaves b as undefined when it is eventually printed. I know the way these callback functions are supposed to be used is to keep chaining of the callback function since that's the only place where the contents of body can be accessed. However, I don't want to keep chaining off functions because it completely destroys the structure of the program. I want to keep all my node server commands inside the http.createServer block. I don't want to place them in functions called from inside the callback function. In this example it doesn't really make sense for the process to be asynchronous since there's only 1 get request anyway and it can't be displayed in console.log until it's received anyway.

I just need a simple way to scrape data with get requests. What would be perfect is if I had some function that I could give a bunch of links, it gets the raw html from them, and then it waits for them to all be done so that I can process all the data at once.

How can something like this be implemented in Node.js?


Solution

  • You can do that using this module: sync-request. With this module you will be able to make synchronous web requests from your NodeJS code.