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.

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 make some authorizations.

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

We can now 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 | sudo -E bash -
# sudo apt-get install nodejs
# sudo apt-key adv --keyserver hkp:// --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
# 'echo "deb [arch=amd64] 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
# 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.