How to create a simple API using Node.js and MongoDB in 30 minutes

In this tutorial, we are going to create a simple Blog API with Node.js and MongoDB using the Mockless low-code Studio.

Create Mockless Project with Node.js and MongoDB

The focus of this tutorial won’t be on user management and authorization, but there are follow-up tutorials explaining how to integrate Basic Auth or AWS Cognito to implement the authorization flows as well.

As in this tutorial we won’t use docker, to simplify the process of running a MongoDB instance we will host the database on MongoDB Cloud. For this example, we can create a free cluster.

Prerequisites

To successfully follow this tutorial you need to have accounts and be familiar with the following platforms:

On the machine where you want to run the API, you will have to install the latest version of:

Create the Mockless project

First of all, let’s create a new project in Mockless Studio called Blog API. The Mockless project creation is a multi-step form so you will have to create your project using: Node.js programming language, REST protocol, MongoDB database and the Data Mocking plugin exactly how we have shown below:

Create the Data Model Structure

The next step would be to create a data model structure. In this step, we should create the entities containing all the properties based on our use-case and we should add all the validators and relations between them.

For now, we would need three entities: User, Article and Comment. Please see below the properties that we created and the relationship between them.

Create the REST endpoints

Now that we have created the entities, the next step would be to create the REST endpoint to able to access the data.

We will need the following endpoints:

GET /articles
#Get all articles

POST /articles
#Add a new blog article

DELETE /articles/{articleId}
#Remove a blog article

GET /articles/{articleId}/comments
#Get all comments from an article

POST /articles/{articleId}/comments
#Add a new comment

DELETE /articles/{articleId}/comments
#Remove a comment

Generate production like data (optional)

As we have the crud endpoints to get, add, update and remove our data. If you remember we have installed a Data Mocking plugin when we have created the project but we haven’t used it so far. The Data Mocking plugin is useful for generating test data or even a large amount of data for performing stress testing with DB data as close as possible to the production one.

This step is not required but it’s recommended as this automatically generates some mock data for our environments and we can better test it later on.

Create the MongoDB instance

For this basic example, we are going to use a hosted service to have a running MongoDB instance as quickly as possible. In case you already have MongoDB running on your local machine or you are familiar with docker, feel free to use it. In case you want to create a new instance, start by creating a free account here and follow the steps shown below:

Create the environment

The environments section help us to run the API in an isolated context, with different DB connection, auth instances and different CI/CD settings.

The next step would be to create at least one environment which will automatically create all the scripts required to run the DB migration scripts, automatically insert the mock data into the environment’s DB and scripts to build and start the server.

Go to the Environments list section under the Development section and click on the “Add environment” button. Here you will have to create one environment called Local, set the MongoDB credentials and set to add 10 mocks data for each entity as shown below:

Connect to your Github account

Now that we have the environment as well, is time to connect our project to a Github account to be able to commit and push the source code to a git repository.
This step is an easy one as well, all we have to do is to add a Github Personal Token to the Mockless project as shown below:

Build the project

The Mockless build process is responsible to identify all the changes made in Mockless Studio from the last build and to update all the affected source code files.
In our case, this first build will create all the source code files of our API and will push the commit to the connected Github account.

To start the build process, go to the Builds section and once the build is successfully created, you can review the created source code under the Review section or directly to your Github account.

Start the project locally

The last step from this tutorial explains how to clone the Github repository and start the project in your local environment.

First of all, you have to get your repository URL and clone it into your local environment.

git clone https://github.com/$YOUR_USERNAME/$YOUR_PROJECT_NAME.git

After you have the source code created by Mockless on your local environment, you will have to run an init.sh bash script that is responsible to install all the npm dependencies, decrypt your secrets environment variables and build your project.

cd $YOUR_PROJECT_NAME
./init.sh local

*Note Depending on your OS, you might have to grand extra permissions to the init.sh script to be able to properly start it.

cd $YOUR_PROJECT_NAME
chmod 777 init.sh

Once you run the script, you will be prompted to enter the encryption password that you set once you have created your environment.

The next step would be to run the DB migrations and seeds to prepare the MongoDB database and insert the generated mock data. Just run the following script:

npm run local:mongoDb:migrate

Now is the time to finally start it. The only thing that you have to run is the following script and the API will be up and running on port 3001.

npm run local:express:start

Next steps

For now, we have just created a simple API but as you saw the API is not protected at all, anyone can perform any kind of actions and we do not have a simple way to deploy it on a cloud account.

To have a scalable and secure API will require some extra work. In the following tutorials, you can see how to secure your API, add user management, create docker containers for a better development experience and how to deploy it to your AWS account using Serverless.