MEAN Stack tutorial with examples
The MEAN stack is a popular web development stack that includes MongoDB, Express, Angular, and Node.js. It's a full-stack JavaScript framework for building dynamic, scalable web applications. In this tutorial, we will create a simple MEAN stack application that allows users to add, view, update, and delete tasks (CRUD operations) from a database.
Prerequisites:
- Node.js and npm installed.
- Angular CLI installed (for setting up Angular).
- Basic understanding of JavaScript, TypeScript, and web development.
Overview of the MEAN Stack:
- MongoDB: NoSQL database for storing data in a JSON-like format.
- Express: Web application framework for Node.js.
- Angular: Frontend framework for building Single Page Applications (SPA).
- Node.js: JavaScript runtime environment for the backend.
Step 1: Setting Up the Backend (Node.js + Express + MongoDB)
1.1. Create a new directory for the project.
1.2. Initialize Node.js Project.
1.3. Install Dependencies.
You'll need the following dependencies:
- express: For creating the server.
- mongoose: For MongoDB object modeling.
- cors: For handling cross-origin requests.
- body-parser: For parsing JSON requests.
1.4. Create the Server (app.js)
Create an app.js
file to set up the Express server and connect to MongoDB.
1.5. Create the MongoDB Model (models/task.js)
In the models/
directory, create a task.js
file to define the task schema using Mongoose.
1.6. Run the Backend Server
To run the server, use the following command:
Now your backend is running on http://localhost:3000.
Step 2: Setting Up the Frontend (Angular)
2.1. Install Angular CLI
If you don't have the Angular CLI installed globally, you can install it with:
2.2. Create a New Angular Project
Navigate back to your root directory and create a new Angular project.
2.3. Install Angular HTTP Client Module
You need the Angular HttpClientModule to make HTTP requests.
2.4. Create a Service to Communicate with the Backend
In Angular, we’ll create a service to communicate with the backend API.
In src/app/task.service.ts
, add the following code:
2.5. Create a Task Component
Now, create a component to manage and display tasks.
In src/app/task-list/task-list.component.ts
, modify it as follows:
2.6. Create Task List Template
In src/app/task-list/task-list.component.html
, add the HTML for the task list:
In src/app/styles.scss
, add the following CSS to style the tasks:
2.7. Add HttpClientModule and FormsModule
Finally, ensure that HttpClientModule
and FormsModule
are imported in your app.module.ts
:
2.8. Run the Angular App
To run the Angular application, use the following command:
Your Angular frontend will now be running on http://localhost:4200.
Step 3: Testing the MEAN Application
- Backend is running on http://localhost:3000 (Node.js + Express).
- Frontend is running on http://localhost:4200 (Angular).
Test the full CRUD operations:
- Add tasks from the Angular app.
- View tasks on the frontend.
- Update tasks by toggling completion status.
- Delete tasks by clicking the delete button.
Conclusion
In this tutorial, you've learned how to create a simple MEAN stack application that allows you to add, view, update, and delete tasks using MongoDB, Express, Angular, and Node.js. The full stack allows you to handle both the frontend and backend of a web application, all with JavaScript and TypeScript.
You can expand this application by adding features like user authentication, validation, and more complex data models.
Comments
Post a Comment