Simple REST API using Express, & MongoDB -Project Setup

Sunday, March 22, 2020

Simple REST API using Express, & MongoDB -Project Setup




decided to build a simple REST API using Express & MongoDB. This simple application is built on the following points.

  • A simple program to observe subscriber details.
  • A User schema is built using MongoDB to store user details in the database.
  • HTTP requests are sent from the REST API that will allow to work with this simple program.
Let's Dive into the project

(Codes and Terminal commands will be written in italic)

  1. As the very first step it is necessary to set up a new npm package - npm init
  2. Some dependencies are needed to be installed 
- npm i express mongoose (install express and MongoDB)

-npm i --save -dev dotenv nodemon (install the developer dependencies)


After the required dependencies are installed , let's move on to the project structure.






  • users.js contains the user schema for the user to be created in the DB.

  • Within the routes folder route.rest is implemented to check the REST APIs and the users.js contains all the routes.

  • .env file contains all the environment variables.






After creating your project as above , a simple change must be done in the package.json to activate the nodemon dependecy.

Under scripts change the test name to any word you prefer and the value it denotes to be nodemon server.js

Next we will be moving on to the .env file where the environment variables are stored.

  • I have given a variable inside the .env file to declare the url to the database,
DB_URL=mongodb://localhost/(dbName)



That is all for the project setup, I will be implementing the code in the next blog















0 comments :

Post a Comment