Create a Node.js function for an HTTP GET request with URL parameters

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 7 years ago
Updated a year ago

In this lesson, you will learn how to create a function in your Node.js Todo API server that gets the ID of the Todo item being requested from the URL and retrieve the Todo item from Elasticsearch.

Instructor: [0:01] In our Swagger API specification, we built in an endpoint that accepts an ID on the ToDo path. We'll retrieve a single ToDo by that ID. The parameters it accepts are the ID itself, which is pulled in from the path.

[0:18] Then that's going to look in the find ToDo by ID file, according to the Swagger router controller. It's going to look for a function called find ToDo by ID, so let's create all that. In my controllers folder, I'll create a new file, call it find ToDo by ID.

[0:35] I'm going to create my client using the helper function that we created earlier, and then I'll export the module. Create the function has the request and response parameters. The first thing we'll do is just write out a log statement so we can see the server side.

[0:55] Now that the ID for the ToDo that we're looking for, we're passing in as a parameter on the URL.

[1:04] We can access that using the req.swagger.params object.ID value. That parameter name ID comes from right here where we defined that in the specification. We'll open up a getRequest to our Elasticsearch client or with our Elasticsearch client.

[1:26] Specify the index of ToDo, and the index type of ToDo. Then pass in the ID number that we're looking for. Again, that's going to be request.swagger.params.ID.value. We'll provide a callback that has an error and a response object.

[1:50] Actually, the first thing we'll do is we'll just assign our headers because we're going to need those no matter what we do from here. We want to specify in the header that the Content Type is application/json. Then we'll check to see if that error object exists.

[2:07] If it does, we're going to send that error back down to the client. If it doesn't exist, then we're going to send the results that we got back from our Elasticsearch query. We'll find those under response._source. Let me do this to show you why I put _source on there.

[2:27] I'm actually going to remove that, and then save it. Then I'm going to go over to my console here and issue a curl command to my API server. Then we had that /ToDo endpoint and then it accepted the ID number of the ToDo that I wanted back. If I issue that now, I get the raw results.

[2:49] Let's actually do that again, but this time we're going to pipe it through JQ so that it looks nicer. You can see I got the full Elasticsearch results back. I got the index, the type, the version number, and all that kind of stuff.

[3:00] The part that I really only care about is this part in the _source section. That's the ToDo that the client is expecting back. That's why in my response here, I just sent back the response ._source. If I save that again, I can rerun that same curl command.

[3:18] Now I get back just the ToDo itself, which is what the client is expecting. That's also what conforms to the ToDo schema that we defined in our Swagger specification.