Giter Site home page Giter Site logo

express-practise's Introduction

Express JS Practice

Get Request :

app.get("/",  (req, res) => {
  res.status(202).send("Hello bro!!!");
})

Get Request with params :

app.get("/api/users/:id",(req,res)=>{
  console.log(req.params);
  const parseID = parseInt(req.params.id);
  if(isNaN(parseID)) {
    return response.status(400).send({msg : "bad Request. Invalid ID"})
  } 
  const findUser = mockUserData.find(user=>user.id===parseID);
  if(!findUser)  { 
    return res.status(404).send({msg : 'User not found'})
  }
  return res.send(findUser);
})

Post Request with body :

app.post("/api/users", (req,res)=>{
  console.log(req.body);
  const {body} = req;
  const newUser = {id : mockUserData[mockUserData.length - 1].id+1, ...body};
  mockUserData.push(newUser)
  return res.status(201).send(mockUserData);
})

Put Request with body :

app.put("/api/users/:id",(req,res)=>{
  const {
    body,
    params: {id},
  } = req;
  const parseID = parseInt(id);
  if(isNaN(parseID)) return res.sendStatus(400);
  const findUserId = mockUserData.findIndex((user)=>user.id === parseID);
  if(findUserId===-1)  return res.sendStatus(404);
  mockUserData[findUserId]={id:parseID, ...body};  console.log(mockUserData[findUserId]);
  return res.sendStatus(200);
});

Patch Request with params :

app.patch("/api/users/:id", (req,res)=>{
  const {
    body,
    params : {id},
  }= req;
  const parseID = parseInt(id);
  if(isNaN(parseID)) return res.sendStatus(400);
  const findUserId = mockUserData.findIndex((user)=>user.id == parseID);
  if(findUserId===-1) return res.sendStatus(404);
  mockUserData[findUserId] = {...mockUserData[findUserId], ...body};  console.log(mockUserData[findUserId]);
  return res.sendStatus(200);
})

Delete Request with params :

app.delete("/api/users/:id", (req,res)=>{
  const {
    params : {id},
  } = req;
  const parseID = parseInt(id);
  if (isNaN(parseID)) return res.sendStatus(400)
  const findUserId = mockUserData.findIndex(user => user.id == parseID)
  if (findUserId === -1) return res.sendStatus(404)  mockUserData.splice(findUserId,1 );
  return res.sendStatus(200);  
})

express-practise's People

Contributors

harshit-paneri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.