I always wondered what is NodeJS, Express and MongoDB and why are they getting popular. Truth to be told I got scared of them because I had almost zero knowledge about them. I was familiar with server-side programming so I gave it a go.

I remember when I eventually picked up the courage to try, I had such a hard time understanding the documentations for Express, MongDB and Node that I gave up. However I kept trying and soon I wrapped my mind around these amazing technologies.

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 a database. This is the place where you store information for your web websites (or applications).

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 respectively).

To learn the fundamentals of NodeJS, Express and MongoDB I built a noncomplex app. After setting up the environment and basic Express server on local machine I filled the DB with dummy data and connected it to my app.

NOTE: This is not a tutorial.

The dummy data looked like this.

{
   title: 'Advanced Object Oriented Programming',
   code: '2030',
   year: 2,
   term: 'SU',
   credit: 3  
}

Then I wrote a Model file for the the data to be entered in the database. I used an ORM for Mongo called MongooseJS which is widely used to access the DB easily.

Model

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

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

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

In my app.js file I then wrote all four basic CRUD functions.

CREATE

// POST - Crud
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.term = req.body.term;
    newCourse.credit = req.body.credit;
    
    newCourse.save(function (err, course) {
        if(err){
            res.send('error saving course');
        }
        else{
            console.log(course);
            res.send(course);
        }
    })
});

READ

// GET - cRud
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

// PUT - crUd
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

// DELETE - cruD
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);
         }
     }
    );
});

Testing using Postman

After completing building the application I carried out tests using Postman which is a Chrome plugin. It basically sends HTTP requests to a URL of our choosing. We can even pass in parameters and use all four CRUD operations. Everything worked like it was supposed to.

You can find the code at

aamirsahmad / CRUDapp