DEV Community

Cover image for Writing Your First REST API
Adarsh Pandya
Adarsh Pandya

Posted on

Writing Your First REST API

REST API

so this is the second part to my previous post 'Understanding REST api',in this part we'll make a small REST - API in node JS. so without further ado let's start making your first ever API

TOOLS

  1. install node js (https://nodejs.org)
  2. install vs code (editor)
  3. install POSTMAN (for testing routes)

Steps

  • create a folder
  • initialize the folder with
npm init -y
Enter fullscreen mode Exit fullscreen mode
  • open the folder in vscode
  • type ctrl+shift+` in your keyboard to open integrated terminal
  • we'll be using expressjs for writing api
  • so in terminal type npm install express uuid
  • uuid will help us to create a RFC random id
  • in package.json go to scripts and replace tests with "scripts":{ "start":"node app.js" }

LET'S start

create a file called app.js in your root folder
inside app.js write the following code

Alt Text

all of our routes are stores in folder called routes/routes.js
inside routes.js write the following code

Alt Text

how to check whether request working or not?
open the postman , if you want a get request ,select GET from a dropdown , and type endpoint in url section

you can checkout the code at : https://github.com/WhoAdarshPandya/rest_for_beginners

in upcoming blogs , we'll cover how to deploy a complete rest api on heroku :)

Top comments (0)