CodeX
Published in

CodeX

Photo by Chris Ried on Unsplash

Everything about Rest API

1. HTTP GET Method

2. HTTP POST Method

3. HTTP PUT Method

4. HTTP DELETE Method

  1. NPM (or Yarn) Installed — To create a Node Application.
  2. Postman — Postman is basically an HTTP Client Software that helps to Create and Test the Rest of API Services.
code .
npm init
npm i --save express nodemon
node_API           - node_modules
- index.js
- package.json
- package-lock.json
const express = require('expres');const app = express();app.use(express.json());//HTTP GET Method, to fetch the data from Server to Client.app.get("/",(req,res) => {res.send("Hello Developers!")})app.listen(9000,() => { return "Server starts..."})
nodemon
http://localhost:9000

GET API

const users = [{ "username" : "Sundar Pichai", "id" : 1},{ "username" : "Elon Musk", "id" : 2},{ "username" : "Jeff Bezos", "id" : 3},{ "username" : "Bill Gates", "id" : 4},]
app.get("/api/users", (req,res) => {res.send(users);})

POST API

app.post("/api/users/post" , (req,res) => {username = req.body.usernameif(username){res.send({msg:"Success"})}return res.send({msg:"Error"})})
{"username" : "Smith"}

PUT API

app.put("/api/user-update/:id" , (req,res) => {const input_id = req.params.id;const input_username = req.params.username;const userUpdate = users.find(n => n.id === parseInt(input_id))if(!userUpdate){res.send("ID Invalid")}const {Error} = {msg:"Error"};if(Error){res.send({msg:"Error"})}else{users.username = input_username;res.send({msg:"Success"})}})

DELETE API

app.delete("/api/delete-user/:id", (req,res) => {const input_id = users.find(n => n.id === parseInt(req.params.id))if(!input_id){res.send("ID Invalid!")}const index = users.indexOf(users)users.splice(index,1)res.send(input_id)})

--

--

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store