The service layer pattern helps organize your code by separating business logic from request handling. It makes your app easier to read and maintain.
Service layer pattern in Express
class ServiceName { async methodName(params) { // business logic here } } module.exports = new ServiceName();
The service class contains methods with your app's main logic.
Route handlers call these service methods to keep routes simple.
class UserService { async getUserById(id) { // fetch user from database } } module.exports = new UserService();
class OrderService { async createOrder(orderData) { // validate and save order } } module.exports = new OrderService();
This example shows a simple Express app using a service layer. The UserService holds user data and logic to find a user by ID. The route handler calls the service method to get the user and sends the response. This keeps the route clean and focused on HTTP details.
const express = require('express'); const app = express(); app.use(express.json()); // Service layer class UserService { constructor() { this.users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; } async getUserById(id) { return this.users.find(user => user.id === id) || null; } } const userService = new UserService(); // Route handler app.get('/users/:id', async (req, res) => { const id = Number(req.params.id); const user = await userService.getUserById(id); if (user) { res.json(user); } else { res.status(404).json({ message: 'User not found' }); } }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));
Keep service methods focused on business logic only.
Services can be reused in different routes or parts of your app.
Testing services separately is easier than testing routes with logic mixed in.
The service layer pattern separates business logic from route handling.
It helps keep your code organized, clean, and easier to maintain.
Use services to reuse logic and simplify testing.