Develop Express.js backend APIs with controllers, services, and routes. Use when creating or modifying Node.js backend endpoints, business logic, or Express middleware.
When developing backend APIs for the quinipolo project:
Structure
File Organization
quinipolo-be/controllers/ (PascalCase, e.g., NotificationController.js)quinipolo-be/services/ (PascalCase, e.g., NotificationService.js)quinipolo-be/routes/ (lowercase, e.g., notifications.js)Code Patterns
// Controller Pattern
async functionName(req, res) {
try {
const result = await ServiceName.method(params);
res.status(200).json({ success: true, data: result });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ success: false, error: error.message });
}
}
Error Handling
{ success: boolean, data/error: any }Integration
app.js around line 58Service Method:
async sendPushNotification(tokens, title, body, data) {
// Business logic here
return result;
}
Controller Method:
async registerToken(req, res) {
try {
const { token, deviceInfo } = req.body;
await NotificationService.registerDeviceToken(req.user.id, token, deviceInfo);
res.status(200).json({ success: true });
} catch (error) {
res.status(500).json({ success: false, error: error.message });
}
}
Route Definition:
router.post('/tokens', authenticate, NotificationController.registerToken);
router.get('/', authenticate, NotificationController.getNotifications);