Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Store the Response of a GET Request In a Local Variable In Node JS

Tags:

node.js

get

I know the way to make a GET request to a URL using the request module. Eventually, the code just prints the GET response within the command shell from where it has been spawned.

How do I store these GET response in a local variable so that I can use it else where in the program?

This is the code i use:

var request = require("request");
request("http://www.stackoverflow.com", function(error, response, body) {
    console.log(body);
}); 
like image 534
SheikhZayed Avatar asked May 27 '15 14:05

SheikhZayed


People also ask

How do I handle GET and POST request in node JS?

Note: If you are going to make GET, POST request frequently in NodeJS, then use Postman , Simplify each step of building an API. In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package. Web Server: Create app.

How does node js store data locally?

Storing your Node. js application's configuration data is quite simple - every object in JavaScript can be easily rendered as JSON, which in turn is just string data that can be sent or saved any way you'd like. The simplest way to do this involves the built-in JSON. parse() and JSON.

How do you pass a body in GET request in node?

You can not add body to a get request, you will have to add a query string for the request to send those data. GET can take body with the request, Thanks for your effort.

How do I send response back in node JS?

setHeader('Content-Type', 'text/html'); this line will set the format of response content o text/html. write() function method on response object can be used to send multiple lines of html code like below. res. write('<html>'); res.


1 Answers

The easiest way (but it has pitfalls--see below) is to move body into the scope of the module.

var request = require("request");
var body;


request("http://www.stackoverflow.com", function(error, response, data) {
    body = data;
});

However, this may encourage errors. For example, you might be inclined to put console.log(body) right after the call to request().

var request = require("request");
var body;


request("http://www.stackoverflow.com", function(error, response, data) {
    body = data;
});

console.log(body); // THIS WILL NOT WORK!

This will not work because request() is asynchronous, so it returns control before body is set in the callback.

You might be better served by creating body as an event emitter and subscribing to events.

var request = require("request");
var EventEmitter = require("events").EventEmitter;
var body = new EventEmitter();


request("http://www.stackoverflow.com", function(error, response, data) {
    body.data = data;
    body.emit('update');
});

body.on('update', function () {
    console.log(body.data); // HOORAY! THIS WORKS!
});

Another option is to switch to using promises.

like image 87
Trott Avatar answered Oct 02 '22 16:10

Trott