Ứng dụng demo API REST CRUD với Flask và MongoDB Atlas — phục vụ học tập.
flask-example/
├── app/
│ ├── __init__.py # Application factory
│ ├── extensions.py # Kết nối MongoDB
│ ├── routes/
│ │ └── student_routes.py
│ ├── services/
│ │ └── student_service.py
│ └── utils/
│ └── serializers.py
├── postman/
│ └── Flask_MongoDB_CRUD.postman_collection.json
├── config.py
├── run.py
├── requirements.txt
└── .env.example
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
copy .env.example .envChỉnh file .env với connection string MongoDB của bạn.
python run.pyServer chạy tại: http://localhost:5001
| Method | URL | Mô tả |
|---|---|---|
| GET | / |
Health check |
| GET | /api/students |
Lấy danh sách |
| GET | /api/students/{id} |
Lấy theo ID |
| POST | /api/students |
Tạo mới |
| PUT | /api/students/{id} |
Cập nhật toàn bộ |
| PATCH | /api/students/{id} |
Cập nhật một phần |
| DELETE | /api/students/{id} |
Xóa |
{
"name": "Nguyen Van Minh",
"email": "minh@example.com",
"age": 22,
"major": "Computer Science"
}- Mở Postman → Import → chọn
postman/Flask_MongoDB_CRUD.postman_collection.json - Chạy Create Student trước (request sẽ tự lưu
student_id) - Test các request còn lại
- Không commit file
.envlên Git - Nếu đã lộ connection string, đổi mật khẩu user trên MongoDB Atlas