Hello Developers If you want to build modern web applications using one of the most powerful JavaScript tech stacks, the MEAN Stack is the perfect place to start. In this guide, you’ll build your first MEAN Stack app—step-by-step—using MongoDB, Express.js, Angular, and Node.js.
Whether you're a beginner or improving your full-stack skills, this tutorial gives you the clarity and confidence you need to start building real-world applications.
Introduction — Why Build a MEAN Stack App?
The MEAN Stack is trending because:
It uses one language (JavaScript) for both frontend and backend also for databases.
It is fast, scalable, and perfect for modern web apps
It has a huge community, libraries, and tools
It’s used by companies like PayPal, Netflix, and LinkedIn
Building your first MEAN app helps you understand the full development lifecycle, from backend APIs to frontend UI and deployment.
This tutorial will guide you through each step with clarity and practical examples.
Prerequisites — What You Need Before Starting
Before you start coding, make sure the following tools are installed:
1. Node.js (Latest LTS Version)
Download from: https://nodejs.org/
2. Angular CLI
Install using:
npm install -g @angular/cli
3. MongoDB Community Server / MongoDB Atlas
Local installation or cloud-based MongoDB Atlas.
4. A Code Editor
Preferably VS Code.
5. Basic Understanding
- JavaScript
- Terminal commands
- JSON APIs
If you have these ready, let’s start building your first MEAN app!
Project Overview — What We Will Build
In this tutorial, we will build a simple Task Manager App where users can:
Add tasks
Retrieve tasks
Mark tasks as completed
Delete tasks
This app will help you understand the interaction between:
Node.js/Express.js → Backend API
MongoDB → Database
Angular → Frontend UI
Step 1: Setup — Initialize the Project
1. Create Folder Structure
mean-task-app/
├── backend/
└── frontend/
2. Initialize Backend (Node + Express)
mkdir backend
cd backend
npm init -y
npm install express mongoose cors
3. Create Server File
Create server.js and add:
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const app = express();
app.use(express.json());
app.use(cors());
mongoose.connect('mongodb://localhost:27017/mean_tasks');
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Your backend is now ready to expand.
Step 2: Backend — Node.js + Express + MongoDB
1. Create MongoDB Schema
Create models/Task.js:
const mongoose = require('mongoose');
const TaskSchema = new mongoose.Schema({
title: String,
completed: {
type: Boolean,
default: false
}
});
module.exports = mongoose.model("Task", TaskSchema);
2. Create REST APIs
Create routes/tasks.js:
const express = require('express');
const Task = require('../models/Task');
const router = express.Router();
router.post('/add', async (req, res) => {
const task = await Task.create(req.body);
res.json(task);
});
router.get('/', async (req, res) => {
const tasks = await Task.find();
res.json(tasks);
});
router.put('/:id', async (req, res) => {
const updated = await Task.findByIdAndUpdate(req.params.id, req.body);
res.json(updated);
});
router.delete('/:id', async (req, res) => {
await Task.findByIdAndDelete(req.params.id);
res.json({ message: "Task deleted" });
});
module.exports = router;
3. Connect Route in server.js
const taskRoutes = require('./routes/tasks');
app.use('/tasks', taskRoutes);
API is ready!
Step 3: Frontend — Angular Components
1. Create Angular Project
ng new frontend
cd frontend
ng serve
2. Create a Service for API
ng generate service task
task.service.ts:
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class TaskService {
url = "http://localhost:3000/tasks";
constructor(private http: HttpClient) {}
getTasks() {
return this.http.get(this.url);
}
addTask(task: any) {
return this.http.post(this.url + "/add", task);
}
deleteTask(id: string) {
return this.http.delete(`${this.url}/${id}`);
}
}
3. Create UI Component
ng generate component task
task.component.html:
<h2>Task Manager</h2>
<input [(ngModel)]="title" placeholder="Enter task">
<button (click)="addTask()">Add Task</button>
<ul>
<li *ngFor="let t of tasks">
{{ t.title }}
<button (click)="delete(t._id)">Delete</button>
</li>
</ul>
Your Angular frontend is ready.
Step 4: Integration — Connect Angular with Backend
In task.component.ts:
import { Component, OnInit } from '@angular/core';
import { TaskService } from '../task.service';
@Component({
selector: 'app-task',
templateUrl: './task.component.html'
})
export class TaskComponent implements OnInit {
tasks: any = [];
title: string = "";
constructor(private taskService: TaskService) {}
ngOnInit() {
this.loadTasks();
}
loadTasks() {
this.taskService.getTasks().subscribe(res => {
this.tasks = res;
});
}
addTask() {
if (this.title.trim()) {
this.taskService.addTask({ title: this.title }).subscribe(() => {
this.loadTasks();
this.title = "";
});
}
}
delete(id: string) {
this.taskService.deleteTask(id).subscribe(() => {
this.loadTasks();
});
}
}
Now your frontend and backend communicate perfectly.
Step 5: Testing — Test the Application
1. Start Backend
node server.js
2. Start Angular
ng serve
3. Open Your App
Visit:
http://localhost:4200
Test the following:
Add tasks
View tasks
Delete tasks
Refresh page to confirm data persists
Everything should work seamlessly.
Step 6: Deployment — Deploy to Production
Option 1: Deploy Backend
Use Render, Railway, or Vercel Serverless Functions
Or deploy on AWS EC2, DigitalOcean, or Linode
Option 2: Deploy Angular App
Run:
ng build --prod
Host using:
Netlify
Vercel
Firebase Hosting
GitHub Pages
Option 3: Use a Single Server
You can use:
NGINX
PM2
Reverse Proxy Setup
This gives you a complete full-stack deployment setup.
Conclusion — Next Steps and Learning Path
Great job!
You just built your first MEAN Stack application from scratch.
What you learned today:
Setting up Node.js & Express backend
Creating MongoDB schemas and APIs
Building Angular UI components
Connecting frontend to backend
Running, testing, and deploying your MEAN app
Next Steps
Add user authentication (JWT)
Improve UI using Angular Material
Add task status, filters, and categories
Learn advanced MongoDB queries
No comments:
Post a Comment