Categories
AWS JavaScript Programming Tutorials Web Dev

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 comment here below and share it on your preferred social networks, thank you!

By Mirko Benedetti

Hi there, my name is Mirko Benedetti and I'm a Software Developer. Excellence is what I consider to be our ultimate goal, and passion for technology constantly drives me to it. I began programming self-taught at a very young age; since then I learned a lot and every day I love learning new things. Here is my Linkedin Curriculum, feel free to connect.

Leave a Reply

Give me your opinion, I will be grateful.