How To Build REST API Using Express js (Node).......?-PART-1

Hello world,

First of all we need to understand what is REST...? So basically REST stands for Representational State Transfer, which is software architecture style defines set of constraints for creating web-services. You can go on this url to know more about the Rest.

Back to our heading How to Build RESTfull API using Express....? We basically refers 4 request to implement RESTfull api which are,

1.get -for reading data

2.post -for creating data

3.Put -for updating data

4.delete-for data deletion

first we need to initialize project

npm init -y

Next we need express js which is web framework build using HTTP module of Node.js

for installing express js using terminal

npm i express

First we need to import in our file

const express=require('express')

express return the function which we have to call it and store it in another variable.

const express=require('express')

const app=express()

then we have need to port to listen on our server for that we need to use method named as listen

const express=require('express')
const app=express()

const port=3000
app.listen(port)

for checking server is running on port we need to add callback function to console log the port

const express=require('express')
const app=express()

const port=3000
app.listen(port,()=>{
console.log(`server is running on port ${port}`)
})

now lets get the jump on RESTfull methods in express ,express provides this methods, in every method we need to specify the endpoint(the URL-we are going to use the /api/data url) & callback function which takes two parameters req & res. req and res has build in methods,like req.body ,req.params you can check additional methods here. res.send(send the response to end user) you can check additional methods here

for better understanding we are going to create array locally and perform the crud (Create, Read, Update,Delete) operation without using database which makes code user-friendly for beginner.

1.GET

const express=require('express')
const app=express()

const my_data =[
                { 
                  id:1,
                  name:"xyz"
                },
                {
                 id:2,
                 name:"mnc"
                 }

               ]

//get method

app.get("/api/data",(req,res)=>{

res.send(my_data)

})


const port=3000
app.listen(port,()=>{
console.log(`server is running on port ${port}`)
})

in terminal just type

node filename.js

and head over your browser and type in browser http://localhost:3000/api/data and boom , you can able to see the my_data array in browser. congrats you successfully implemented first route using get method.

disclaimer : if any errors occurred please comment , i will solve it for you or just go towards stack-overflow

2.post

For implementation of post method we need POSTMAN.(Postman is a great tool when trying to dissect RESTful APIs made by others or test ones you have made yourself. It offers a sleek user interface with which to make HTTP requests, without the hassle of writing a bunch of code just to test an API's functionality.)

here we are going to use same endpoint for creating new data . And additionally for post request we need middle-ware function express.json which we are going to learn after some time.

const express=require('express')
const app=express()

// middle-ware function 
app.use(express.json())

const my_data =[
                { 
                  id:1,
                  name:"xyz"
                },
                {
                 id:2,
                 name:"mnc"
                 }

               ]

//get method

app.get("/api/data",(req,res)=>{

     res.send(my_data)

})

//post method

app.post("/api/data",(req,res)=>{
     let newdata={
              id:my_data.length + 1,
              name:req.body.name
           }
     my_data.push(newdata)

    res.send(my_data)

})

const port=3000
app.listen(port,()=>{
console.log(`server is running on port ${port}`)
})

now just type node filename.js and open POSTMAN tab and type URL there which is http://localhost:3000/api/data and choose POST method

2.png

then in body select raw and JSON

3.png

next we have to type name in JSON format like in image and send it

4-c.png

if all is good then response will be back with name and new id

5-c.png

Yes you successfully implemented get and post . we will see put and delete in next blog.

Thank you!!!!!