From the course: Node.js: Web Servers, Tests, and Deployment

Making a request with the request method - Node.js Tutorial

From the course: Node.js: Web Servers, Tests, and Deployment

Making a request with the request method

- [Instructor] We may need to make HTTP requests in order to upload and download files and information from HTTP servers. Node.js has two modules for this, the http module. This is the one used for a request to sites without a security certificate, and the https module, which is going to make a request to a site with a security certificate. So what I want to do here is make a request for some data, and I'm going to make this request to this endpoint here. So you can choose whatever Wikipedia article you want to grab. I've grabbed Cher here, and this is going to be the endpoint that I'll send this request to. The first thing we want to do, inside of our file called index.js, is I'm going to import, I'm going to require, the https module, and the file system module. From here, I want to define an object filled with options, and this options object is going to have a few important keys on it. The first is a host name. So where are we making this request? This is sort of the root of our endpoint here. Nice, and then the next thing that we want is a port. So any of these requests that are running over HTTPS will use port 443, then we'll specify the path. So I want to make the request to wikipedia.org/something. So the path will be here, wiki/Cher, and then we also want to define the method, which is get. We're making a get request for this information. Now, once I've done this, I'm going to call https.request. We'll supply the options as the first argument, and then the callback function is going to be responsible for taking the data from that endpoint and doing something with it. So the first thing we want to do is we want to create a little container, responseBody. This is going to contain all of that data, then we're going to call a response.setEncoding, and the setEncoding function is going to take in UTF-8. So this is our character encoding for that text. Now, here, we'll use response.on. Response.on will take in a value as the first argument. This is going to be data. So this function is going to be called whenever we get some data. Now, when we make this request, it's going to be broken down into chunks. Instead of loading all of this data at once, we're going to instead just grab little bits of it so that that response can come back more efficiently. So let's take a look at what the length of the chunk is. So we'll say chunk.length, then we'll use responseBody += chunk. We'll append that data to our empty responseBody string. Finally, we'll call request.end, closing up that request. Now, we want to make sure that we're navigated to the right folder. (laughs) I often find that the fastest way to do that is to type CD, drag the start folder over here and make sure you're routed to the right place. The other thing I should say is that if you hit Control + Backtick, Control + Tilde, up in your upper left-hand corner of your keyboard, that's going to open and close the embedded terminal in VS Code. So that's pretty cool. Now, to run our node app, we'll say. Now, to run this, we'll say node index. This is going to populate all of these chunks of information to us, and that's pretty cool. We see the length of the chunk. Notice that the last one is a little bit shorter because we have all of these little containers for data, and then at some point we've run out. Let's go ahead and do something with this now. We imported that file system module for a reason, I promise. So what we'll say here is we'll say response.on, end. We want to use the file system module to write to a file. So we'll say fs.writeFile. We'll call the file cher.html. We'll pass responseBody as the second argument, which is going to take that data and populate it into our HTML page, and then let's go ahead and add an error in case something goes wrong. We'll say if there's an error, we'll throw it. Otherwise, we want to just say file downloaded. Perfect, so at this point, we should be able to run this again. Let's type node index, and it says file downloaded. That's sort of exciting, but more exciting would be to see this whole cher.html page. So now we have all of this HTML that we've downloaded from the internet. So we've built a little page scraper to grab some information from that endpoint. A lot of different cool things that you can do with https and fs combined.

Contents