Quick RESTful API with Node.js Express and DocumentDB

In this episode we’ll be continuing our journey through Node.js, looking at a simple and minimal RESTful API example with the help of Express to build it, and DocumentDB for our persistence.

The old plain JavaScript language

If you haven’t done before, take a look at this tutorial which is conceptually similar, except for the fact that persistence this time is based on a MongoDB engine, hosted on AWS, and it’s called DocumentDB.

The first thing we wanna do is to create an EC2 instance, in that DocumentDB is not easily accessible outside its VPC, we will create a private key, run an instance, take note of the instance id, get its IP address:

$ aws ec2 create-key-pair \
      --key-name rest-tutorial-key \
      --query 'KeyMaterial' \
      --output text > rest-tutorial-key.pem

$ aws ec2 run-instances \
      --image-id ami-06d51e91cea0dac8d \
      --instance-type t2.nano \
      --key-name rest-tutorial-key

Consider the fact that instance ids are fake, you’ll have to use your own. And wherever you will find zero filled IPs and security groups in this tutorial, you will have to put your own.

$ aws ec2 describe-instances \
      --instance-ids i-00000000000000000 | grep PublicIpAddress

$ aws ec2 describe-instances \
      --instance-ids i-00000000000000000 | grep GroupId

Let’s authorize traffic inside our VPC and between our machine, in order to SSH, plus let’s authorize the Express server traffic on port 8000. This cidr will be our actual IP address, our security group will the one of our EC2 instance:

$ aws ec2 authorize-security-group-ingress \
      --group-id sg-00000000 \
      --protocol tcp \
      --port 22 \

$ aws ec2 authorize-security-group-ingress \
      --group-id sg-00000000 \
      --protocol tcp \
      --port 8000 \

$ aws ec2 authorize-security-group-ingress \
      --group-id sg-00000000 \
      --protocol tcp \
      --port 27017 \
      --source-group sg-00000000

Let’s SSH inside our EC2 instance an make a setup of our virtual machine, we will install Node.js and the MongoDB shell, our IP address will the one of describe-instances:

$ ssh -i rest-tutorial-key.pem ubuntu@
# curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# sudo apt-get install nodejs
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
# 'echo "deb [arch=amd64] http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" > /etc/apt/sources.list.d/mongodb-org-4.0.list'
# sudo apt-get update
# sudo apt-get install -y mongodb-org-shell
# mkdir restful-express-mongo
# cd restful-express-mongo
# wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
# exit

The second thing to do is to create a cluster and an instance for our database persistence, using our AWS CLI:

$ aws docdb create-db-cluster \
      --db-cluster-identifier rest-tutorial-cluster \
      --engine docdb \
      --vpc-security-group-ids sg-00000000 \
      --master-username master \
      --master-user-password putyourownpassword
$ aws docdb create-db-instance \
      --db-cluster-identifier rest-tutorial-cluster  \
      --db-instance-identifier rest-tutorial-instance \
      --db-instance-class db.r4.large \
      --engine docdb

Our security group will be the same of our EC2 instance. And here we’ll get our database cluster endpoint, we’ll use that on our application to connect.

Issue this command after the creation process is completed, it might take a while:

$ aws docdb describe-db-clusters \
      --db-cluster-identifier rest-tutorial-cluster | grep Address

That’s not all, we have to setup our database and EC2 instance, build and load our application, run it, test it. For the second part of this tutorial click on Page 2 here below.

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.