Students should first follow the experiment guide to proceed with the project
Welcome to the Student Management System project! This repository contains a complete implementation of a CRUD (Create, Read, Update, Delete) application using Node.js, Express, MongoDB, and EJS.
Instead of just looking at the code, use this guide to build the project from scratch yourself. Each section outlines a key component of the application.
The project follows the MVC (Model-View-Controller) pattern, which helps organize code by separating concerns:
- Models: Define the data structure (schema) for MongoDB.
- Views: The user interface templates (EJS) rendered for the browser.
- Controllers: The logic that connects Models and Views, handling requests and responses.
- Routes: Define the URL endpoints for the application.
- Config: Centralized configuration (e.g., database connection).
Create a new directory and initialize your Node project:
pnpm init -yUpdate package.json to use ES Modules by adding "type": "module". Install the dependencies:
pnpm add express mongoose ejs dotenv cors
pnpm add -D nodemonCreate a .env file in the root to store sensitive configuration:
PORT=3000
MONGO_URI=ymongodb+srv://cu:cu@cu.fafaffasfa.mongodb.net/5cCreate a config folder and a db.js file. Use Mongoose to connect to your MongoDB database. Handle connection success and failure gracefully.
Create a models folder and a student.model.js file. The student should have:
name: String, required, trimmed, with length validation.roll: Number, required, unique, must be a positive integer.timestamps: Enabled to track creation and update times.
Create a controllers folder. Implement the following logic:
getStudents: Fetch all students with pagination and sorting.createStudent: Save a new student to the database.getStudentById: Find a specific student.updateStudent: Modify an existing student's details.deleteStudent: Remove a student from the database.
Create a routes folder. Map the controller functions to HTTP verbs and endpoints:
GET /:getStudentsPOST /:createStudentGET /:id:getStudentByIdPUT /:id:updateStudentDELETE /:id:deleteStudent
Create a views folder with a students subfolder.
index.ejs: Display the list of students in a table and provide a form to add new students.edit.ejs: Provide a form to edit an existing student's details.- Tip: Use a CSS framework like Bootstrap to make the UI look professional.
Create routes specifically for rendering the EJS templates. Unlike API routes which return JSON, these will use res.render() to show pages and res.redirect() after actions like creating or deleting.
Create index.js in the root. This is where you connect everything:
- Initialize Express.
- Set EJS as the view engine.
- Register middleware (
cors,express.json,express.urlencoded). - Import and use your API and View routes.
- Listen on the specified port.
- Run the App:
pnpm dev(if you added the script topackage.json). - API: Use Postman or Thunder Client to test the
/studentsendpoints. - UI: Open
http://localhost:3000/view/studentsin your browser.
- Understand how to use Express to create a web server.
- Learn Mongoose for data modeling and MongoDB interaction.
- Implement CRUD operations in a real-world scenario.
- Practice using EJS for server-side rendering.
- Master project organization using the MVC pattern.
Happy coding! π