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 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 \ --cidr 000.00.000.00/00 $ aws ec2 authorize-security-group-ingress \ --group-id sg-00000000 \ --protocol tcp \ --port 8000 \ --cidr 000.00.000.00/00 $ 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 firstname.lastname@example.org # 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.
Hi there. My name is Mirko Benedetti, I’m a Software Developer and I founded this website. 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’m learning new things.