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.
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
