Using request as a function

Now that we have the request module installed, we can start using it. Inside Atom we'll wrap up the section by making a request to that URL, in a new file in the root of the project called app.js. This will be the starting point for the weather application. The weather app will be the last command-line app we create. In the future we'll be making the backend for web apps as well as real-time apps using Socket.IO. But to illustrate asynchronous programming, a command-line app is the nicest way to go.

Now, we have our app file, and we can get started by loading in request just like we did with our other npm modules. We'll make a constant variable, call it request, and set it equal to require(request), as shown here:

const request = require('request');

Now what we need to do is make a request. In order to do this, we'll have to call the request function. Let's call it, and this function takes two arguments:

request({}, () => {

});

In our case, it's going to get called once the JSON data, the data from the Google Maps API, comes back into the Node application. We can add the arguments that are going to get passed back from request. Now, these are arguments that are outlined in the request documentation, I'm not making up the names for these:

In the documentation, you can see they call it error, response, and body. That's exactly what well call ours. So, inside Atom, we can add error, response, and body, just like the docs.

Now we can fill out that options object, which is where we are going to specify the things unique to our request. In this case, one of the unique things is the URL. The URL specifies exactly what you want to request, and in our case, we have that in the browser. Let's copy the URL exactly as it appears, pasting it inside of the string for the URL property:

request({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia',
}, (error, response, body) => {

});

Now that we have the URL property in place, we can add a comma at the very end and hit enter. Because we will specify one more property, we'll set json equal to true:

request({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia',
json: true
}, (error, response, body) => {

});

This tells request that the data coming back is going to be JSON data, and it should go ahead, take that JSON string, and convert it to an object for us. That lets us skip a step, it's a really useful option.

With this in place, we can now do something in the callback. In the future we'll be taking this longitude and latitude and fetching weather. For now, we'll simply print the body to the screen by using console.log. We'll pass the body argument into console.log, as shown here:

request({
url: 'https://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia',
json: true
}, (error, response, body) => {
console.log(body);
});

Now that we have our very first HTTP request set up, and we have a callback that's going to fire when the data comes back, we can run it from the Terminal.