Build your own REST API from scratch - Part 2




In the previous article we have created a basic skeleton for our RESTful API. 
In this part we are going to crack it up a notch by building an actual useful endpoint, status codes, change the static message to a better mechanism, build our controllers, and see how we can retrieve actual useful data via an API call. Ready? Let's keep coding.

For starters, let's change focus by deciding that we are building an Ice Cream shop API. 
WOW, just the thought of that made me hungry...

Oh well, back to work. so the first thing we want to do is to change the static hello message we retrieved to some actual content. For that, we are going to:

Create a JSON data file



Our JSON file will contain an array of objects containing our menu items. We will place this file in a sub-directory in our project which we will call menu_data.



Next up, let's create a separate module to handle our Ice cream menu get method. I will call it iceCream.js and place in an additional directory in our project which I will call menu_modules.



Now let's create the module. 





Let's see what I have done here:


1. I'm requiring the 'fs' module, which is a built in module for accessing and handling physical file system.

2. I'm storing the DATA_FILE path (Which is our JSON file) in a constant variable.

3. I'm creating an iceCream module (Which we will later on export) which will contain a 'get' function. this function follows a Promise pattern, passing in 2 possible callbacks resolve and reject.

3. I'm passing the DATA_FILE to the fs.readFile() method so we can process our data from the JSON file. (As you can see it can be either resolved with the data -(which is the content of the file), or rejected with an error.

If the data is processed successfully it will resolve and parse the file, if not, it will be caught by the reject(error) block and result it what is known as internal server error. (No doubt that we will need to handle our possible exceptions in a later stage).

Next up, we will replace the static message we have in our index.js file with the actual data we want to retrieve from the JSON file. 



So what do we see here?


1. First of all, I have added a require for the iceCream file (On line 5).

2. Secondly, I have changed the basic res.send() with res.status(<status code>).json()

What that will do, is return a status code to the client and attach a JSON response. By the way, the status 200 would be applied by default for a successful call but we will add it anyway for the sake of clearance.

3. The JSON response will contain: 
    a. The status.
    b. the status text (Which is OK for 200).
    c. the actual data from the file that will be sent back to the client.


So what do you say? Shell we see if it worked?


Let's restart our server by executing npm start from the terminal, and get back to our Postman to create a GET request to our route.


Jackpot!, we did it again.
By the way, we could just open the address in our browser (Which would do the exact same thing - send a get request to the endpoint) but what's the fun in that, right? :)

Jokes aside, I think that it is highly important to deeper understand the technology we work with or consume on a daily basis. It is only threw a deeper understanding we can truly flourish in what we do and enrich our toolset.

In part 3 we will continue to build our endpoint, maybe add more methods and routs, the ability to query data or maybe even create a webpage which will help us fetch the data from the server. Hope you liked this part, see you in the next one.





Comments

  1. Appreciation is a wonderful thing...thanks for sharing kepp it up.
    Postman Crack

    ReplyDelete

Post a Comment

Popular posts from this blog

Chromedriver - Under The Hood

Sharing is caring - Intro to Jenkins shared libraries

Build Your Own Custom AMI With Packer