Hello everyone,

In this blog post ill touch on how to create PUT and DELETE routes in an expressJS api. Click this link to see how to create POST and GET routes which i shared in my previous post. So fellas i’ll use my previously created code to implement put and delete methods as well.

'use strict';
 
const bodyParser = require('body-parser'),
 express = require('express');

const app = express();

app.use(bodyParser.json());

app.use(express.static(__dirname));

app.get('/', (req, res, next) => {
 res.sendFile('index.html');
});

const users = [];

app.post('/users', (req, res, next) => {
 const user = req.body;
 user.birthday = new Date(user.birthday);
 user.id = Date.now();
 users.push(user);
 res.json(user);
});

app.listen(3000, err => {
 if (err) {
 console.error(err);
 return;
 }
 console.log('app listening on port 3000');
});

Above code has already implemented server, post and get methods. Add the following code lines in to your code in order to create put request to your api.

app.put('/users/:id', (req, res) => {
 const user = req.body;
 user.updatedValue = user.putRequestValue
 const index = users.findIndex(user => user.id ===
parseInt(req.params.id));
 users[index] = user;
 res.json(users[index]);
});

In above code we have implemented a put method where it will accept an id as parameter and json object as a body and save it as “user” json object then update user.updatedValue to user.putRequestvalue (API Body JSON) then update the json which contains the given parameter id and send only that index of the json.

Lets check that out. Open postman and In postman set method to PUT and set address as localhost:3000/users/ and set body to raw and type to JSON  then type the update value

{


"putRequestValue":"new updated value"

}

And click send, then you will get a response which contains a json and the id is equals to the id which you have passed in parameter and “updatedValue” attribute as “new updated value”

If i explain further more, we are sending a parameter along with api route from postman after localhost:3000/users/ path that is the unique id of our json element by using that id inside of the express api we are going to update a value to a value which we are passing through that api call.

Lets create a delete method in our API.                                                                                       Add this lines of code in to your existing api.

app.delete('/users/:id', (req, res) => {
 const user = req.body;
 const index = users.findIndex(user => user.id ===
parseInt(req.params.id));
 users.splice(index, 1);
 res.sendStatus(200);
});

In above delete method what we have implemented is we are going to call DELETE request through api in postman and the method accept id as a parameter to the method then delete the element which contains that unique id from the json and return status 200 (200 status is used to acknowledge success) as the response. Open postman and In postman set method to DELETE and set address as localhost:3000/users/ and click send button.

You will get a response with 200 status code which means we successfully deleted all the details belongs to that unique id we passed as parameter to the api method.                         And that is how you implement PUT method in your api. In my next blog post I will also share how to use mongo db along with expressJS api. till then good bye 🙂

Brought to you by Yinza’s Class Room

Advertisements