Simple CRUD app using Express and MongoDB

Hi everyone!

Okay so what is CRUD? CRUD is an acronym for Create, Read, Update and Delete. It is a set of operations we get servers to execute (POST, GET, PUT and DELETE ) where we can manipulate our data. What about other key words on this topic? Express and MongoDB?

Express is a framework for building web applications on top of Node.js. It simplifies the server creation process that is already available in Node. In case you were wondering, Node allows you to use JavaScript as your server-side language.

MongoDB is just a database. Where you store information for your web application.

This is not a tutorial, I’m just gonna tell you the basics of manipulating data using CRUD

I wrote a Model file for the the data to be entered in the database as follows,

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var CourseSchema = new Schema({
   title: String,
   code: String,
   year: Number,
});

module.exports = mongoose.model('Course', CourseSchema);

where as you can see there are three variables as title, code and year. Next thing is how do you manipulate those data to create, read, update and delete. Ill functions for each methods separately.

Create

app.post('/courses', function (req, res) {
    var newCourse = new Course();
    
    newCourse.title = req.body.title;
    newCourse.code = req.body.code;
    newCourse.year = req.body.year;
    
    newCourse.save(function (err, course) {
        if(err){
            res.send('error saving course');
        }
        else{
            console.log(course);
            res.send(course);
        }
    })
});

 

Read

app.get('/courses', function (req, res) {
    console.log('getting courses');
    Course.find({})
    .exec(function (err, courselist) {
        if(err){
            res.send('error');
        }
        else{
            console.log(courselist);
            res.json(courselist);
        }
    })
});

 

Update

app.put('/courses/add/:id', function (req, res) {
    Course.findOneAndUpdate({
        _id: req.params.id
    },
    { $set: {title: req.body.title} },
    {upsert: true},
     function (err, newCourse){
         if(err){
             console.log('error occured');
         }else{
             console.log(newCourse);
             res.status(204);
         }
     }
    );
});

 

Delete

app.delete('/courses/remove/:id', function (req, res) {
    Course.findOneAndRemove({
        _id: req.params.id
    },
     function (err, ccc){
         if(err){
             console.log('error occured');
         }else{
             console.log(ccc);
             res.send(204);
         }
     }
    );
});

 

After completing building the application you can test Postman. That is the gist of creating a simple CRUD app using Express and MongoDB.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s