Angular 18 CRUD examples using MEAN stack
Creating a CRUD (Create, Read, Update, Delete) application using the MEAN stack (MongoDB, Express, Angular, Node.js) is a popular way to build full-stack web applications. In this example, we'll set up the back-end API using Node.js, Express, and MongoDB, and the front-end using Angular 18.
Steps:
- Set up the back-end (Node.js/Express/MongoDB)
- Set up the front-end (Angular 18) with CRUD operations
- Connect Angular with Express API to perform CRUD operations
1. Back-End Setup: Node.js, Express, and MongoDB
1.1. Set Up the Back-End Project
Let's first set up the Node.js backend with Express and MongoDB.
Create the Node.js backend folder:
Create the following folder structure:
1.2. Set Up MongoDB Connection (db.js
)
In the config/db.js
file, set up the MongoDB connection.
1.3. Define Todo Model (todo.model.js
)
Create the models/todo.model.js
file to define the Todo model:
1.4. Set Up Routes (todo.routes.js
)
Now, set up the routes to handle the CRUD operations for Todos.
1.5. Set Up Express Server (app.js
)
Create app.js
to configure Express and connect all parts together.
1.6. Start the Backend Server
Run the server by executing:
This will start the backend server on http://localhost:5000
.
2. Front-End Setup: Angular 18 CRUD Application
2.1. Create the Angular 18 Project
Create the Angular project using Angular CLI:
Install dependencies for HTTP requests:
2.2. Create a Todo Service (todo.service.ts
)
This service will interact with the backend API.
2.3. Create the Todo List Component (todo-list.component.ts
)
This component will display the list of Todos.
2.4. Create the Todo Form Component (todo-form.component.ts
)
This component will handle adding and updating Todos.
2.5. Update the app.module.ts
Make sure to import the necessary modules and components.
2.6. Update app.component.html
3. Running the Application
- Start the Backend: Ensure MongoDB is running and then run the Express server:
- Start the Frontend: Run the Angular app:
Visit http://localhost:4200
to see the Todo application.
Conclusion
This is a full-stack MEAN CRUD application with Angular 18 as the front-end and Node.js/Express/MongoDB as the back-end. The application allows users to create, read, update, and delete Todo items with a clean separation between the front-end and back-end logic.
Feel free to enhance this with more features like user authentication, pagination, etc.
Comments
Post a Comment