How PEST.js Works
Understanding the request lifecycle and architecture of PEST.js.
Note:
PEST.js provides a structured, scalable way to handle backend requests while maintaining flexibility.
Request Lifecycle
1
Client Request
Request is sent to the server via API endpoints
POST /api/auth/login
2
Middleware Processing
Security, validation, and logging middleware are applied:
- Rate limiting
- CORS checks
- Authentication
- Request validation
3
Route Handling
Request is routed to the appropriate controller:
router.post('/login', authController.login);
4
Controller & Service
Business logic is executed through services:
// Controller
async login(req: Request, res: Response) {
const user = await authService.validateUser(req.body);
const token = await authService.generateToken(user);
res.json({ token });
}
Key Components
Componentsadd
Middlewareadd
Controllersadd
Servicesadd
Note:
Each component has a specific responsibility:
- Middleware: Request processing and validation
- Controllers: Request/response handling
- Services: Business logic
- Models: Data structure and validation