A simple Express.js API that will manage students and admins users from a mentoring program and will store data in a MongoDB instance
- Install git
- Install node.js (npm will also be automatically installed)
- Install MongoDB and a MongoDB client (GUI from here:
https://www.mongodb.com/docs/manual/installation/#mongodb-installation-tutorials
I prefer using Studio 3T (https://studio3t.com/download/
) as MongoDB client but you can easily use Compass (https://www.mongodb.com/products/compass
). The database server will be:localhost
database port:27017
- Install VS Code or any other code editor that you like.
Method 1: On local environment - without Docker:
- Clone the project.
npm i
- Install all the dependencies.- Manually install nodemon dev dependency by running
npm install nodemon --global
command. - There are a lot dependencies that are not updated frequently in parallel with cypress. So while installing if you are seeing conflicts use
npm i --force
. - Create a .env file on the project root containing 2 secrets: adminSecretKey and secretKey.
e.g.
adminSecretKey=das@5523!da%^DAFSDss123 secretKey=dsadSADXACS3!@(dda12DAD!
- run
nodemon start
command to start the service - Access
http://localhost:1234/api-docs/
to check the documentation of the project.
Method 2: On local environment - with Docker:
- Install Docker desktop
- Clone/pull the project.
- Open a terminal and cd into the project folder
- Run the
docker compose up
command - Check using
docker ps
command if the API and the Mongo DB containers have been raised - Access
http://localhost:1234/api-docs/
to check the documentation of the project.
Method 3: Deploy the API on an Amazon EC2 instance - with Docker
-
Create an Amazon AWS account.
-
Search for EC2 using the search bar and navigate to the EC2 console.
-
Click on the Launch instance button.
-
Configure the instance that you want to create like this if you want to be free tier eligible:
-
In the Key pair section create a new key pair ( the name can be the same as the selected region) and save the .pem file locally. You will need this later on in order to connect via SSH on the EC2 instance.
-
Click on the Create instance button.
-
Navigate to the previously created instance and wait until the Connect button is no longer greyed out ( it will take about 5 minutes).
-
Click on the Connect button and then go to the SSH client tab.
-
Open/bring the previously opened terminal back in focus and cd into the folder where you downloaded the .pem file at step 5. Then make sure that your key is publicly viewable by running
chmod 400 <name_of_your_pem_file>
command. e.g. `chmod 400 eu-central-1.pem'. -
Copy the example SSH client command from the Amazon AWS site and then press enter. You will also be asked if you want to continue. Type yes and then enter. The SSH connection should be established and the console output should look like:
-
Run the following commands via SSH:
11.1.
sudo yum update -y
11.2.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
.Try to execute
nvm --version
command in the terminal just to check if the nvm has been successfully installed. If the output isnvm: command not found
then typeexit
and then enter and after that connect again via SSH with your instance and try again.11.3.
nvm install 16
(or replace 16 with the latest version of node. Check: https://nodejs.org/en/).Check if the node and npm has been successfully installed by executing the following commands:
node --version
andnpm --version
.11.4. Port forward your traffic from port 80 of the instance to the port on which the API is running (
1234
by default) by executing:sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 1234
.11.5. Install git:
sudo yum install git -y
and then verify viagit -v
if it has been successfully installed.11.6. Install Docker:
sudo yum update && sudo yum install docker -y
.11.7. Add group membership for the default ec2-user so you can run all docker commands without using the sudo command:
sudo usermod -a -G docker ec2-user
id ec2-user
newgrp docker
.11.8. Install docker-compose:
wget https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)
sudo mv docker-compose-$(uname -s)-$(uname -m) /usr/local/bin/docker-compose
sudo chmod -v +x /usr/local/bin/docker-compose
.11.9. Enable docker service at AMI boot time:
sudo systemctl enable docker.service
.11.10. Start the Docker service:
sudo systemctl start docker.service
.11.11. Get the docker service status on your AMI instance, run:
sudo systemctl status docker.service
The output should look something like that:11.12.
mkdir projects && cd projects
.11.13.
git clone https://github.com/radu-iulian/express-mongo-jwt-bcrypt-swagger-api.git
.11.14.
cd express-mongo-jwt-bcrypt-swagger-api/
.11.15. Build and run Docker containers by executing the following commands:
docker compose build --no-cache
anddocker compose up -d
.11.16. Check using
docker ps
command if the API and the Mongo DB containers have been raised. -
Access
http://<EC2_instance_IP>/api-docs/
to check the documentation of the project. e.g. http://35.159.24.124/api-docs/. -
If you will want to play with the API using Swagger you will also need to modify the
server.js
file on the EC2 cloned repo so that theoptions
constant includes your EC2 instance in theservers
array.
Example:
You can play around with the API by either launching the Swagger page or by importing the API collection and environment variables into Postman. You can find the Postman exported files in the 'Postman' folder on the project root.