๐Ÿงฑ Full-Stack Boilerplate Setup (Express + MongoDB) โ€” Beginner Friendly Guide

Lovely SharmaLovely Sharma
4 min read

This guide walks you through setting up a full-stack boilerplate using Node.js, Express, and MongoDB with modern project structure, so you can focus on building features โ€” not struggling with setup.


๐Ÿ› ๏ธ 1. Project Initialization

Step 1: Create a new folder and initialize the project

mkdir my-fullstack-app
cd my-fullstack-app
npm init
  • Press Enter to go through the prompts OR manually add your name, version, etc.

  • This creates a package.json file where all your dependencies and scripts are listed.

Step 2: Create the main entry file

touch index.js

This is where your backend server starts running.


๐Ÿ—‚๏ธ 2. Enable import Syntax (ES Module)

By default, Node.js uses require() (CommonJS), but modern apps often prefer import.

To use import, update your package.json:

{
  "type": "module"
}

๐Ÿ” 3. Set Up Environment Variables

Step 3: Install dotenv to manage environment variables

npm install dotenv

Step 4: Create a .env file

touch .env

Add your variables:

PORT=3000
MONGO_URL=your_mongo_connection_string
BASE_URL=http://localhost:3000

Then load them in your code:

import dotenv from "dotenv";
dotenv.config();

๐Ÿš€ 4. Set Up Express

Step 5: Install Express

npm install express

Step 6: Basic Express server setup

// index.js
import express from "express";
import dotenv from "dotenv";
dotenv.config();

const app = express();
const port = process.env.PORT || 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

๐Ÿ” 5. Enable Auto Reload with Nodemon

Nodemon restarts the server when files change.

npm install -D nodemon

Update your package.json scripts:

"scripts": {
  "start": "nodemon index.js"
}

Now start your server:

npm run start

๐Ÿงฐ 6. Use CORS and JSON Middleware

Step 7: Install and configure CORS

npm install cors

Then use it in index.js:

import cors from "cors";

app.use(cors({
  origin: process.env.BASE_URL,
  credentials: true,
  methods: ["GET", "POST", "DELETE", "OPTIONS"],
  allowedHeaders: ["Content-Type", "Authorization"]
}));

Step 8: Parse JSON and Form Data

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

This makes sure your backend can read incoming data properly.


๐Ÿ›ข๏ธ 7. Connect to MongoDB with Mongoose

Step 9: Install Mongoose

npm install mongoose

Step 10: Setup Database Connection

Create a folder called utils and inside it a file db.js:

mkdir utils
touch utils/db.js

Inside utils/db.js:

import mongoose from "mongoose";

const db = () => {
  mongoose.connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("โœ… Connected to MongoDB"))
  .catch((err) => console.error("โŒ DB Connection Error", err));
};

export default db;

Step 11: Import and call db function in index.js:

import db from "./utils/db.js";
db();

๐Ÿง  8. Create a Model (User Example)

Step 12: Create a model folder and a User schema

mkdir model
touch model/User.model.js

Inside User.model.js:

import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
  },
  email: {
    type: String,
    required: true,
    unique: true
  }
});

const User = mongoose.model("User", userSchema);
export default User;

๐Ÿ“ฆ 9. Controller + Routes Folder Structure

Use MVC-style separation: Models, Controllers, Routes.


Step 13: Create a controller

mkdir controller
touch controller/user.controller.js

Inside user.controller.js:

const registerUser = async (req, res) => {
  res.send("User registered!");
};

export { registerUser };

Step 14: Create routes

mkdir routes
touch routes/user.routes.js

Inside user.routes.js:

import express from "express";
import { registerUser } from "../controller/user.controller.js";

const router = express.Router();

router.get("/register", registerUser);

export default router;

Step 15: Use your routes in index.js

import userRoutes from "./routes/user.routes.js";

app.use("/api/v1/users", userRoutes);

Visit this URL in browser or Postman:

http://localhost:3000/api/v1/users/register

Youโ€™ll see: "User registered!"


๐Ÿงพ Final Project Structure (Suggestion)

my-fullstack-app/
โ”œโ”€โ”€ controller/
โ”‚   โ””โ”€โ”€ user.controller.js
โ”œโ”€โ”€ model/
โ”‚   โ””โ”€โ”€ User.model.js
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ user.routes.js
โ”œโ”€โ”€ utils/
โ”‚   โ””โ”€โ”€ db.js
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ index.js
โ”œโ”€โ”€ package.json

โœ… What Youโ€™ve Built

You now have:

  • ๐Ÿ”น Express backend with modular structure

  • ๐Ÿ”น MongoDB connected using Mongoose

  • ๐Ÿ”น Routes and Controllers separated

  • ๐Ÿ”น .env config for secrets

  • ๐Ÿ”น Nodemon + ESModules support

  • ๐Ÿ”น JSON & CORS handling

You're now ready to build out your backend features, connect it to a frontend, or deploy to the cloud.

0
Subscribe to my newsletter

Read articles from Lovely Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lovely Sharma
Lovely Sharma

Hello! I'm Lovely, an undergraduate B.Tech student with a knack for exploring the world of technology and engineering. Through my blog, I document my learnings, experiences, and insights as I navigate my academic journey. Join me as I delve into the fascinating realms of tech, share my projects, and reflect on the lessons learned along the way. Let's embark on this adventure of growth and discovery together!