Building a Production-Ready Backend: Node.js, Express, and MongoDB Atlas Setup Guide (2023) - Part 1

Initializing a Project with Folder Structure and .env Configuration

Prerequisites

Before proceeding with the backend setup, ensure that you have Node.js and npm installed on your machine.

Step 1: Initialize the Project

Start by creating a new directory for your project:

mkdir your-backend-app-name

Navigate to the project directory in your terminal:

cd your-backend-app-name

Next generate npm project using following command:

npm init

Or to generate an empty npm project without going through an interactive process use:

npm init -y

To make the project compatible with ES6 import statements, add "type": "module" to your package.json file.

This sets up a new Node.js project with default settings.

Step 2: Folder Structure

Here is the recommended folder structure for your backend application:

Folder Structure

Let's break down the folder structure:

  • public: This folder is typically used for static assets that your server will serve, such as images, stylesheets, or client-side JavaScript files.

  • src: The main source code folder for your application.

    • controllers: Modules responsible for handling the logic of your application. Each route may have its own controller.

    • db: Database-related files, such as database connection setup.

    • middlewares: Middleware functions that can be used in your routes. Middleware functions have access to the request and response objects and can perform actions before the main handler is called.

    • models: Used for defining the structure of your data, such as database models or other data-related structures.

    • routes: Define the routes of your application. Each route typically has its own file or module within this folder, handling incoming requests, using controllers to process them, and sending responses.

    • utils: Utility functions or modules that can be used across different parts of your application.

    • app.js: The main application file where you set up your Express application, define middleware, and connect routes.

    • constants.js: A file to store constants used throughout your application.

    • index.js: The entry point of your application, typically responsible for starting your server.

  • .env: A configuration file for storing environment variables, which can include sensitive information or configuration settings for your application.

  • .env.sample: A template for the .env file, providing guidance on what variables need to be set and their expected values. This file can be committed to version control to assist other developers in setting up their environments.

  • .gitignore: Specifies files and folders that should be ignored by version control, commonly including node_modules (where Node.js packages are installed) and .env.

  • .prettierignore: Specifies files and folders that should be ignored by the Prettier code formatter.

  • .prettierrc: Configuration file for Prettier, defining code formatting rules.

  • package.json: Contains metadata about your project and the dependencies it uses, including scripts for common tasks such as starting the server or running tests.

Step 3: .env Configuration

Install the dotenv package to load environment variables from the .env file. Additionally, set up npm scripts for starting the server in both production and development environments.

  1. Install the dotenv package and include it as a dependency:

     npm install dotenv
    

    Install the nodemon package and include it as a devDependency:

     npm install -D nodemon
    
  2. Update your package.json file with the following scripts:

     "scripts": {
       "start": "node src/index.js",
       "dev": "nodemon -r dotenv/config --experimental-json-modules src/index.js"
     }
    

    The "dev" script uses nodemon with the dotenv configuration to enable automatic reloading during development.

  3. Create a .env file in your project root and add your configuration variables:

     # .env
    
     KEY=VALUE
    

    Replace KEY and VALUE with your actual environment variables and their values.

In conclusion, this structure provides a modular and organized foundation for building a Node.js backend application.

In next steps we are going to configure MongoDB Atlas and additional steps depending on your project requirements.