Explore Related Tools
Hand-picked utilities and calculators related to this tool.
As your API grows, putting logic directly inside route files becomes a nightmare. Master the art of Decoupling.
The route file should only define where requests go. The controller handles what happens.
// routes/userRoutes.js
router.get('/:id', userController.getUser);
// controllers/userController.js
exports.getUser = async (req, res, next) => {
const user = await UserService.findById(req.params.id);
res.json({ data: { user } });
};For standard CRUD, don't repeat yourself. Use a generic handler factory.
const deleteOne = (Model) => async (req, res) => {
await Model.findByIdAndDelete(req.params.id);
res.status(204).json({ data: null });
};
// Usage
exports.deleteUser = deleteOne(User);
exports.deleteProduct = deleteOne(Product);Don't put complex business rules in your controllers. Move them to aService Layer. This makes your API logic testable without a database.
Hand-picked utilities and calculators related to this tool.
