DAY 6 BackEnd

πŸ—‚οΈ Setting Up Files and Folders for a Node.js + MongoDB Project

When starting a backend project using Node.js and MongoDB, having a clear file and folder structure is key to keeping your code organized, readable, and scalable. In this post, we’ll walk through setting up the basic structure for a clean and maintainable project.


πŸ“ Step 1: Project Initialization

First, create your project folder and initialize it:

mkdir video-tube-backend
cd video-tube-backend
npm init -y

This creates a package.json file which stores project metadata and dependencies.

Perfect! You're aiming to write the first blog post focusing on setting up a professional backend project β€” not the code yet, just the structure. Here's a clean and detailed blog post draft based on your reference points.


🧠 Step 2: Create the Entry Point

In the root of the project, create:

touch index.js

This will be the main file that starts your server (we’ll wire it up in the next blog).

πŸ—οΈ Step 3: Folder & File Structure

Here’s the recommended structure:

video-tube-backend/
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ db/
β”‚   β”‚   └── index.js          // Handles MongoDB connection
β”‚   β”œβ”€β”€ constants.js          // Stores reusable constants like DB name
β”‚   └── app.js                // Express app setup 
β”‚
β”œβ”€β”€ .env                      // Environment variables
β”œβ”€β”€ index.js                  // Entry point of the application
β”œβ”€β”€ .gitignore                //Prevents certain files/folders from being pushed to GitHub.
β”œβ”€β”€ package.json              // Project config and dependencies
└── README.md                 // Optional: Project overview

πŸ—οΈ Step 4: Folder Structure

Create a professional src folder structure like this:

src/
β”œβ”€β”€ controllers/
β”œβ”€β”€ db/
β”œβ”€β”€ middleware/
β”œβ”€β”€ models/
β”œβ”€β”€ routes/
β”œβ”€β”€ utils/
β”œβ”€β”€ app.js
β”œβ”€β”€ constants.js

Each folder has a clear responsibility:

  • controllers/ β†’ Handle business logic

  • db/ β†’ Database connection

  • middleware/ β†’ Custom Express middleware

  • models/ β†’ Mongoose or other data models

  • routes/ β†’ API route definitions

  • utils/ β†’ Helper functions/utilities

  • app.js β†’ Express app setup

  • constants.js β†’ Reusable constants like DB name

At this stage, don’t create any files inside these folders yet. Just set up the structure.

🧽 Step 5 : Setup .gitignore

Create a .gitignore file to ignore unnecessary files in version control. You can generate one from gitignore.io:

Here’s a common one:

# Node
node_modules/
.env
.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Editor settings
.vscode/
.idea/

πŸ“„ Step 6 : Use Modern import Syntax

To enable import/export (ES Modules) instead of require, update your package.json:

{
"type": "module"
}

This tells Node.js to treat your project as an ES module system.


🧰 Step 7 : Install Dev Tools βœ…

Install development dependencies to help with productivity:


Run the following command to install development dependencies:

npm install -D nodemon prettier

πŸ“¦ Dev Tools Explained:

  • nodemon: Automatically restarts your server when code changes are detected.

  • prettier: Automatically formats your code to maintain consistency.


βš™οΈ Create a Prettier Configuration File

Create a file named .prettierrc in the root of your project and add the following:

{
  "semi": true,
  "singleQuote": true,
  "printWidth": 80
}

This config will:

  • Add semicolons (semi: true)

  • Use single quotes instead of double quotes (singleQuote: true)

  • Wrap lines longer than 80 characters (printWidth: 80)

Set up done.βœ…βœ…βœ…


wait for Day 7 to connect DataBase.


0
Subscribe to my newsletter

Read articles from ADARSH KUMAR PANDEY directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ADARSH KUMAR PANDEY
ADARSH KUMAR PANDEY