Quick RESTful API with Node.js Express and DocumentDB

Let’s now create a database and a collection in it, using our MongoDB shell:

$ ssh -i rest-tutorial-key.pem ubuntu@00.000.00.00
# cd restful-express-mongo/
# mongo --ssl --host rest-tutorial-cluster.cluster-000000000000.us-west-2.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username master --password putyourownpassword
rs0:PRIMARY> use rest-tutorial
rs0:PRIMARY> db.createCollection("articles")
rs0:PRIMARY> exit

Let’s download the dependecies for our project:

# npm init
# npm install express --save
# npm install mongodb --save
# exit

Let’s go ahead and build our code, index.js, to implement all the main verbs a RESTful API can have. Let’s begin by coding the create verb, we will use the http post method of Express:

app.post('/end-point', (req, res) => {

    var values = {
        title: req.param('title'),
        author: req.param('author'),
        body: req.param('body'),
        url: req.param('url')
    };

    connection(function (err, db) {

        if (err) throw err;
        var dbo = db.db("rest-tutorial");

        dbo.collection("articles").insertOne(values, function (err, result) {
            if (err) throw err;
            res.send(result);
            db.close();
        });

    });

});

Let’s have a look at what we have created, through meas of our RESTful API read verb. We will use the http get method:

app.get('/end-point', (req, res) => {

    var title = {};
    if(req.param('title') != undefined)
            title = { title:  req.param('title') };

    connection(function (err, db) {

        if (err) throw err;
        var dbo = db.db("rest-tutorial");

        dbo.collection("articles").find(title).toArray(function (err, result) {
            if (err) throw err;
            res.send(result);
            db.close();
        });

    });

});

We can now update our document and use the http put method of our RESTful API:

app.put('/end-point', function (req, res) {

    var title = req.param('title');
    var query = { title: title };

    var values = {
        title: title,
        author: req.param('author'),
        body: req.param('body'),
        url: req.param('url')
    };

    connection(function(err, db) {

        if (err) throw err;
        var dbo = db.db("rest-tutorial");

        dbo.collection("articles").updateOne(query, { $set: values }, function(err, result) {
            if (err) throw err;
            res.send(result);
            db.close();
        });

    });

});

We won’t forget to delete our documents through our API, via the http delete method of Express:

app.delete('/end-point', function (req, res) {

    var query = { title: req.param('title') };

    connection(function (err, db) {

        if (err) throw err;
        var dbo = db.db("rest-tutorial");

        dbo.collection("articles").deleteOne(query, function(err, result) {
            if (err) throw err;
            res.send(result);
            db.close();
        });

    });

});

And here we’re loading our index.js which is our complete application on our EC2 instance, for the full code of it please go to my GitHub.

$ scp -i rest-tutorial-key.pem index.js ubuntu@00.00.000.000:/home/ubuntu/restful-express-mongo

We have done a nice job, but it’s time to test our API. We will make use of Postman to accomplish this task. But before of that, let’s start our application:

$ ssh -i rest-tutorial-key.pem ubuntu@00.000.00.00
# cd restful-express-mongo/
# node index.js

I’m gonna show you just this screenshot, just to make an example. You’ll be able to do the rest.

Choose the right method and the right end-point, you’re gonna also have to change localhost to our actual EC2 IP address:

Postman RESTful API Express MySQL
If you can’t read it clearly, just download it and make a zoom with your viewer.

If you want to read the full code of this tutorial, please go to this GitHub repository. Should you have any more doubts, do not hesitate to contact me in person.

In this tutorial you learned al little bit of Express and an example of a minimal RESTful API.

I’m looking forward to see you again on this blog, and I’ll be back soon with new ideas and new topics to work with. Till then, keep coding hard and stay tuned here.

Did you like this post? Please, share it on your preferred social networks or comment here below, thank you!

6 thoughts on “Quick RESTful API with Node.js Express and DocumentDB

  1. What’s up all, here every person is sharing these kinds of know-how, thus it’s good to read this blog, and I pay a visit to it everyday.

  2. Extraordinary Shella,
    you must be one of my best readers.
    Keep in touch

  3. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your site? My blog site is in the exact same area of interest as yours and my visitors would certainly benefit from some of the information you provide here. Please let me know if this ok with you. Regards!

  4. Hi Ray,
    you certainly can do so. The content of this blog is under CC BY-ND license, as stated in the navigation bar. This means that you can borrow some of its content as long as you mention the source (www.benedict.cloud) and do not alter that content.

  5. When someone writes an piece of writing like this… This article is amazing. Thanks!

  6. Hello Lizette,
    You’re full of compliments here. They’re welcome, but take also the time to express your opinion on technical matters and pose questions you might have about it.
    I’d be very grateful!

Leave a Reply

Give me your opinion, I will be grateful.