REST API Tutorial

Restful API with Express – Part II

Summary of learning

This is the second part of Restful CRUD API with Express, MongoDB and Mongoose. I am going to let you know add a record, edit a record and delete a record using nodejs and mongodb.

This is the second part of Restful CRUD API with Express, MongoDB and Mongoose. I am going to let you know add a record, edit a record and delete a record using nodejs and mongodb.

I’ll create a restful API with express and nodejs.

This node js tutorial will cover the following features:

  • How to create a record using Nodejs.
  • How to delete a record in Mongodb using Nodejs.
  • How to update a record in Mongodb using Expressjs.

CRUD API Using Nodejs, Express and NodeJS

Let’s create methods that help to add a record, edit a record and delete a record.

Create A Record into MongoDB

We will develop a new Rest API to create a new employee data entry into a Mongodb table. Because we’ll be sending JSON data to the node server, I’ll make a POST type Rest request.

app.post('/api/employees', function(req, res) {
    // Add a record into collection
    Employee.create({
        name : req.body.name,
        salary : req.body.salary,
        age : req.body.age
    }, function(err, employee) {
        if (err)
            res.send(err);

        // featch all employees
        Employee.find(function(err, employees) {
            if (err)
                res.send(err)
            res.json(employees);
        });
    });

});

Employee.create mongo method is use to create a new document into the employee collection.

Update Record into Mongodb Using Node JS

We’ll use nodejs and express to develop a new PUT type Restful api request to update data in the MongoDB database. We must provide the employee id, which will be used to update the record in the table.

We created json data and sent it to Mongoose ORM using Employee.findByIdAndUpdate(). This function requires two parameters: the employee id to update and the updated data in json format.

app.put('/api/employees/:employee_id', function(req, res) {
    let id = req.params.employee_id;
    var data = {
        name : req.body.name,
        salary : req.body.salary,
        age : req.body.age
    }

    // save the user
    Employee.findByIdAndUpdate(id, data, function(err, employee) {
    if (err) throw err;

    res.send('Successfully! record has been updated - '+employee.name);
    });
});

Delete Record from Mongodb collection

To remove employee records from the Mongodb collection, we’ll make a new DELETE Type rest API request with node js. Employee id will be sent as a parameter, which you want to remove from the Mongodb collection.

// delete a employee by id
app.delete('/api/employees/:employee_id', function(req, res) {
    console.log(req.params.employee_id);
    let id = req.params.employee_id;
    Employee.remove({
        _id : id
    }, function(err) {
        if (err)
            res.send(err);
        else
            res.send('Successfully! record has been Deleted.'); 
    });
});
If you have requirements that needs our attentioN

Please feel free to contact us

We would love to hear from you

Share with your network

Facebook
Twitter
LinkedIn
WhatsApp
Email

This tutorial is intended to cover all the essentials steps and insights to understand Restful API with Express – Part II

Genislab Technologies

Learn further