REST and REST APIs - Part 2

REST and REST APIs - Part 2

In this blog, we will set up a REST API implemented using HTTP to play around and understand.

Resources

  1. API endpoint: https://hob-api.vercel.app/

  2. GitHub Repository: https://github.com/sbk2k1/API-Blog

Prerequisites

We will need some tools to get through this blog. They are:

  1. Git/GitHub and npm/yarn(Necessary only if the deployed endpoint is down)

  2. Postman (We can use cURL but Postman will provide us with a cleaner UI)

Installation

  • Git/GitHub (optional)- Check out my blog!

  • npm and yarn (optional)- To install npm, follow the below steps:

    1. Download the Node.js installer from here.

    2. Run the installer and follow the installation steps.

    3. Once installed, open a command prompt or terminal and type npm -v to check the version of npm.

To install yarn, follow the below steps:

  1. Download the Yarn installer from here.

  2. Run the installer and follow the installation steps.

  3. Once installed, open a command prompt or terminal and type yarn -v to check the version of Yarn.

Note: Yarn can also be installed using npm by running the command npm install -g yarn in the command prompt or terminal.

  • Postman: To install Postman, you can follow these steps:

    1. Go to the Postman website.

    2. Click the "Download" button for the version of Postman you want to install.

    3. Follow the on-screen instructions to download the installation file.

    4. Once the file is downloaded, open it and follow the instructions to install Postman.

    5. Once the installation is complete, you can launch Postman by opening the application from your computer's application menu or by double-clicking on the Postman icon on your desktop (if you chose to create one during installation).

That's it! You're now ready to use Postman to test and explore APIs.

Setting up a local server (optional)

Follow the following steps to start up a server on your local system:

  1. Clone the repository:

     git clone https://github.com/sbk2k1/API-Blog.git
    
  2. Navigate to the directory:

     cd API-Blog
    
  3. Install dependencies using either npm or yarn:

     npm install
    

    or

     yarn install
    
  4. Remove the two forward slashes (//) from the beginning of the app.listen line to uncomment it.

     app.listen(3000, () => {
       console.log(`Server running on port 3000`)
     });
    
  5. Add two forward slashes (//) at the beginning of the module.exports = app the line to comment it out.

     // module.exports = app;
    
  6. Once the dependencies are installed, start the server:

     npm start
    

    or

     yarn start
    

    This will start the server on port 3000 by default.

Getting it to work

Note: I'm going to use the deployed endpoint for the blog, but you can use the http://localhost:3000 if you have it set up in your local system.

/ Route

Create a new request in Postman as shown below:

Fire it off and see what happens.

This is the response.

This signifies that the server is working fine. Note that the status in the top right that says 200 OK. (Status code refresher here)

You can also paste the link into your browser and see what happens. By default, when you enter a URL into a web browser and press enter, it sends a GET request to the server to retrieve the content of the specified resource.

We will look into each route and each little element in greater detail in the next blog. Stay tuned for part 3. If you still have any questions/queries you can reach out to me on my LinkedIn / GitHub / Twitter.

Cheers!

Did you find this article valuable?

Support High on Bugs! by becoming a sponsor. Any amount is appreciated!

Β