Shakil Logo

Command Palette

Search for a command to run...

0
Blog
PreviousNext

How to Start a Professional Backend Project (Complete Step-by-Step Guide)

Learn how to initialize, structure, and run a modern backend project with Node.js, Express, and MongoDB including all commands, folder structure, and best practices.

Introduction

Starting a backend project the right way is important for scalability, maintainability, and performance. In this guide, we will build a professional backend project from scratch using:

  • Node.js
  • Express.js
  • MongoDB
  • Mongoose
  • Environment variables
  • Proper folder architecture
  • API routing
  • Middleware
  • Error handling

By the end of this tutorial, you will have a production-ready backend starter project.


1. Install Required Software

Before starting, make sure the following tools are installed.

Node.js

Download and install from:

https://nodejs.org

Check installation:

node -v
npm -v

Git

git --version

MongoDB

Install locally or use MongoDB Atlas Cloud Database.

https://mongodb.com


2. Create Backend Project Folder

Create a new project directory.

mkdir professional-backend
cd professional-backend

Initialize Node project:

npm init -y

This will create:

package.json

3. Install Core Dependencies

Install required backend packages.

$ pnpm add express mongoose dotenv cors

Install development dependencies:

$ pnpm add -D nodemon

Package Explanation

PackagePurpose
expressBackend server framework
mongooseMongoDB ODM
dotenvEnvironment variables
corsAPI access control
nodemonAuto restart server

4. Update package.json Scripts

Edit package.json.

"scripts": {
 "dev": "nodemon src/server.js",
 "start": "node src/server.js"
}

Run development server:

$ pnpm dev

5. Create Professional Folder Structure

Create the following folder structure.

mkdir src
mkdir src/config
mkdir src/controllers
mkdir src/models
mkdir src/routes
mkdir src/middleware
mkdir src/services
mkdir src/utils

Final structure:

professional-backend
│
├── src
│   ├── config
│   ├── controllers
│   ├── models
│   ├── routes
│   ├── middleware
│   ├── services
│   ├── utils
│   └── server.js
│
├── .env
├── package.json

6. Create Express Server

Create file:

src/server.js
import express from "express"
import cors from "cors"
import dotenv from "dotenv"
 
dotenv.config()
 
const app = express()
 
app.use(cors())
app.use(express.json())
 
app.get("/", (req, res) => {
  res.send("Backend Server Running")
})
 
const PORT = process.env.PORT || 5000
 
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`)
})

7. Enable ES Modules

Update package.json.

"type": "module"

This allows using import/export syntax.


8. Create Environment Variables

Create .env file in root.

PORT=5000
MONGO_URI=your_mongodb_connection_string

Never commit .env to Git.

Create .gitignore:

node_modules
.env

9. Connect MongoDB Database

Create:

src/config/database.js
import mongoose from "mongoose"
 
const connectDB = async () => {
  try {
    await mongoose.connect(process.env.MONGO_URI)
 
    console.log("MongoDB Connected")
  } catch (error) {
    console.error("Database Error:", error)
    process.exit(1)
  }
}
 
export default connectDB

Update server.js.

import connectDB from "./config/database.js"
 
connectDB()

10. Create Example Model

Create:

src/models/User.js
import mongoose from "mongoose"
 
const userSchema = new mongoose.Schema(
  {
    name: String,
    email: String,
    password: String,
  },
  { timestamps: true }
)
 
export default mongoose.model("User", userSchema)

11. Create Controller

Create:

src/controllers/userController.js
import User from "../models/User.js"
 
export const createUser = async (req, res) => {
  try {
    const user = await User.create(req.body)
    res.status(201).json(user)
  } catch (error) {
    res.status(500).json({ message: error.message })
  }
}
 
export const getUsers = async (req, res) => {
  const users = await User.find()
  res.json(users)
}

12. Create API Routes

Create:

src/routes/userRoutes.js
import express from "express"
import { createUser, getUsers } from "../controllers/userController.js"
 
const router = express.Router()
 
router.post("/", createUser)
router.get("/", getUsers)
 
export default router

13. Register Routes in Server

Update server.js.

import userRoutes from "./routes/userRoutes.js"
 
app.use("/api/users", userRoutes)

Now your API endpoints are:

GET /api/users
POST /api/users

14. Add Error Handling Middleware

Create:

src/middleware/errorMiddleware.js
const errorHandler = (err, req, res, next) => {
  res.status(500).json({
    message: err.message,
  })
}
 
export default errorHandler

Add to server:

import errorHandler from "./middleware/errorMiddleware.js"
 
app.use(errorHandler)

15. Test API with Postman

Example request:

POST

http://localhost:5000/api/users

Body:

{
  "name": "John",
  "email": "john@email.com",
  "password": "123456"
}

16. Add Development Tools

Install useful tools.

$ pnpm add morgan helmet

Example usage:

import morgan from "morgan"
import helmet from "helmet"
 
app.use(morgan("dev"))
app.use(helmet())

17. Production Ready Improvements

For real production apps add:

  • Authentication (JWT)
  • Validation (Zod / Joi)
  • Rate limiting
  • Logging
  • Caching
  • Docker
  • Testing (Jest)

18. Run Backend Project

Start development server:

$ pnpm dev

Production:

npm start

Server will run:

http://localhost:5000

Final Folder Structure

professional-backend
│
├── src
│   ├── config
│   │   └── database.js
│   │
│   ├── controllers
│   │   └── userController.js
│   │
│   ├── models
│   │   └── User.js
│   │
│   ├── routes
│   │   └── userRoutes.js
│   │
│   ├── middleware
│   │   └── errorMiddleware.js
│   │
│   └── server.js
│
├── .env
├── .gitignore
├── package.json

Conclusion

In this guide, we built a professional backend project from scratch including:

  • Project initialization
  • Dependency installation
  • Clean architecture
  • MongoDB integration
  • Controllers and routes
  • Error handling
  • Environment configuration

This structure can easily scale into large production applications.

You can now extend it with:

  • Authentication system
  • Role based access
  • Payment systems
  • File upload
  • Microservices architecture

Happy Coding 🚀