Beginner Dev Documentation: Node.js RESTful API with Mongoose

Aditya Aufar
5 min readSep 3, 2022

--

I’ve been a software developer for a few years but Node.js is not actually my tech stack. I give it a try to learn it mainly because I am always interested to learn what’s the trend nowadays. After that, who knows what opportunity lies ahead.

However, one of my biggest weaknesses is to remember things. That’s why I created this article as a documentation of my own that maybe at the same time can help others to learn the same thing.

I am sure there are millions of tutorials out there that already cover RESTful API in Node.js but I choose to follow this tutorial on Youtube by Brad Traversy because that’s what works best for me. So some of the stuff is probably already covered in the video but not in my article, including the concept of RESTful API. The aim in this tutorial is to create a Goal Repository web app.

The Tools Used

These tools are assumed to be already installed in your computer

  1. VS Code
  2. Node. js
  3. Postman

Initial Folder and File Setup

Initially, these files and folders should be created in the root folder. In this tutorial the root defined as MERN-TUTORIAL folder

  1. backend folder : This contains all backend related files and folders
  2. .env file : Also described as Environment Variables file, usually contains key or variable that has secret value
  3. .gitignore file : List of files or folders that going to be ignored to be pushed to Git. For example node_modules folder

After that, create server.js file inside backend folder. So the file tree should look like this.

Initial Step

Run this command in root folder or MERN-TUTORIAL/

npm init

After that, couple of questions will be asked, you can skip all by keep pressing Enter key on your keyboard except the one question that ask about the entry point(index.js). Fill that one question with “server.js”.

A file called package.json will be created as the result of the command. Initially, the package.json file will look like this.

{"name": "mern-tutorial","version": "1.0.0","description": "","main": "server.js","scripts": {"test": "echo \"Error: no test specified\" && exit 1",},"author": "","license": "ISC",}

Initial Packages

Couple of NPM packages are needed in this tutorial. Here are the commands to install the packages. Make sure to run these commands from the root or MERN-TUTORIAL/

npm i express dotenv mongoose colors express-async-handler

the command above will update the package.json to look like this

After that, run this command to add this package just for development environment.

npm i -D nodemon

the command above will update the package.json to look like this

Additional Changes to Run Script

After nodemon package is installed, package.json needs to be updated manually in order for nodemon to run the server and watch any changes. The additional code should be added in the scripts part in the package.json file.

Now the server can run with nodemon by using this command.

npm run server

The REST of The File Structure

Below is the REST (no pun intended) of the files and folders that needed to be created manually inside the backend folder. The clearer view of the structure can be seen in this github repository https://github.com/aufaraditya/RESTful-NodeJS-Tutorial

Database Setup

In this tutorial, MongoDB is used to store the data. Basically, MongoDB generates an URL that can be used to connect between the web app and the database. In order to obtain the URL, we must register an account with additional configuration afterwards. The youtube video explains the steps to obtain the URL. This documentation will assume the URL already obtained. The URL is then put in the .env file.

The configuration of the database is contained in the db.js file in the path backend/config/db.js. The visualisation of how to setup the database is as follows

Schema Setup

After connection between the web app and the database has been established, the next step is to create the schema. Since the purpose of this tutorial is to create a goal repository app, so the schema is about the goal with the attribute of text and timestamp. The goal schema then becomes the Model object and used mainly in the Controller or in goalController.js file in the path backend/controllers/goalController.js.

The REST

Server.js always be the starting point of the web app according to the server script in the package.json. However the REST (no pun intended) of the process mainly just the communication between the Routes from the goalRoutes.js file in the path backend/routes/goalRoutes.js and the Controller from the goalController.js file in the path backend/controllers/goalController.js. The routes are called via the Postman app with the appropriate actions (GET, POST, UPDATE, DELETE) then each action has different functions accordingly.

Check out the youtube video mentioned above for more detailed information regarding the code or you can see this github repository https://github.com/aufaraditya/RESTful-NodeJS-Tutorial where I store the code and the visualization between the code.

In the end, credit always goes to Brad Traversy for creating such an amazing well-explained video. You can check out his Youtube channel that covers a programming tutorial as well as tutorials on how you can be a great programmer.

Hopefully this post is useful in any way.

--

--