0
0
Expressframework~5 mins

Service layer pattern in Express

Choose your learning style9 modes available
Introduction

The service layer pattern helps organize your code by separating business logic from request handling. It makes your app easier to read and maintain.

When you want to keep your route handlers simple and clean.
When your app has complex business rules that need clear organization.
When you want to reuse logic across different parts of your app.
When you want to make testing your business logic easier.
When you want to separate concerns for better teamwork and scaling.
Syntax
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.

Examples
A simple service to get user data by ID.
Express
class UserService {
  async getUserById(id) {
    // fetch user from database
  }
}

module.exports = new UserService();
Service handling order creation logic.
Express
class OrderService {
  async createOrder(orderData) {
    // validate and save order
  }
}

module.exports = new OrderService();
Sample Program

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.

Express
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'));
OutputSuccess
Important Notes

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.

Summary

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.