I would like to create a HTTP request inside a function using the request-json npm package.
Here is the code for the same:
request = require('request-json')
client = request.createClient('http://localhost:5000')
function getData(){
client.get('/currentdata', function(err, res, body){
return console.log(body)
})
}
getData()
This works fine on the console, but when I try return body
instead of return console.log(body)
, and change my function call statement as var x = getData(); console.log(x);
, it gives me undefined
as output.
I would like to know how to return a value from inside the client.get function to the main area, going through the getData() function.