I'm writing an Express/Javascript App and am struggling to find a way of making a variable available for use further into my program. Apologies for possibly a noob question but I'm coming from Ruby and a little stuck on this one. I'm requesting data from a url, successfully storing it into a variable, and hoping to use that data further in the program. The below code is a basic example. I'm not actually requesting the google website.
const data = 'Heyy'
request('http://google.com', (error, response, body) => {
if (!error && response.statusCode === 200) {
const data = response.body
}
return data
})
console.log(data)
I'm going around in circles but here i've initialised a constant, re-assigned it, and was hoping to return it from the request with it's new value and for use elsewhere. console.log(data) still returns it's original value of 'Heyy'. I'm also using the npm library request
and request-promise
but with no success.