Modern App Guidelines
Những điều cần lưu ý khi phát triển một ứng dụng hiện đại.
Các thành phần của app
Định nghĩa
Routes
Controller
Thin, ít logic:
Services
Fat, nhiều logic:
Model
Helper
Định nghĩa và Nguyên tắc
Cấu trúc file Helper trong App
Ví dụ
1
View (Template)
Frontend - Presentation Layer
Hiển thị giao diện cho user
Data từ Controller
HTML rendered
upload.hbs
photo-detail.hbs
home.hbs
2
Client-side JS
Frontend - Interaction Layer
Xử lý tương tác user, validate form, AJAX
User events (click, input)
HTTP requests, DOM updates
upload-validator.js
upload-form.js
3
Route
Backend - Entry Point
Map URL → Controller + Middleware
HTTP Request (URL + method)
Gọi middleware chain → controller
GET /upload
POST /api/photos/upload
4
Middleware
Backend - Pre-processing Layer
Xử lý request trước controller (auth, validate, parse)
Request object
Modified request hoặc Response (nếu fail)
authMiddleware
uploadMiddleware
validationMiddleware
5
Controller
Backend - HTTP Handler Layer
Nhận HTTP request, gọi Service/Model, trả HTTP response
HTTP Request (req, res)
HTTP Response (JSON/HTML)
photoController.upload()
photoController.getPhoto()
6
Service
Backend - Business Logic Layer
Xử lý business logic phức tạp, orchestration
Plain JS data
Plain JS data/object
photoService.upload()
photoService.processImage()
7
Helper
Backend - Utility Layer
Pure functions, utilities dùng chung
Plain data
Transformed data
ValidationHelper
IdHelper
ImageHelper
8
Model
Backend - Data Access Layer
Định nghĩa cấu trúc dữ liệu + CRUD operations
Plain JS data
Database results
Photo.create()
Photo.findById()
9
Database
Persistence Layer
Lưu trữ dữ liệu
SQL queries
Data rows
SQLite tables: photos, users, tags
Modules Relationship
Bonus: Validation Modules
Q: Validation nên ở đâu? Model, Middleware hay Service?
A: Câu hỏi rất hay! Đây là điểm nhiều người nhầm lẫn. Câu trả lời là CẢ 3 ĐỀU CÓ, nhưng mỗi nơi validate MỤC ĐÍCH KHÁC NHAU!
So sánh 3 tầng Validation ở từng modules
Middleware: Route → Controller
Service: Controller → Model
Model: Trước INSERT/UPDATE
Luồng dữ liệu
USE CASE 1: Hiển thị trang Upload
USE CASE 2: User Submit Form Upload
USE CASE 3: Xem chi tiết ảnh
Chi tiết:
Tóm tắt
Cấu trúc thư mục của App
Luồng dữ liệu chính:
Nguyên tắc vàng:
Những quy tắc để quản lý app development
Twitter Tech Tree
Security Tips
Expose API data at client-side
