Create Your First Service

A service encapsulates business logic and keeps controllers clean.

Create UserService

Create src/service/user.service.ts:

import { Provide } from '@midwayjs/core';
@Provide()
export class UserService {
private users = [
{ id: 1, name: 'Tom', email: '[email protected]' },
{ id: 2, name: 'Jerry', email: '[email protected]' },
{ id: 3, name: 'Kate', email: '[email protected]' },
];
async getUsers() {
return this.users;
}
async getUserById(id: number) {
return this.users.find(user => user.id === id);
}
async createUser(name: string, email: string) {
const newUser = {
id: this.users.length + 1,
name,
email,
};
this.users.push(newUser);
return newUser;
}
}

Key points

@Provide()

Registers this class into the IoC container.

Service responsibility

Service should:

  • hold business logic
  • call databases/APIs
  • transform data

Service should not:

  • handle HTTP routing directly
  • depend on ctx for request flow

Async-first style

Service methods are usually async because real apps call I/O resources.

Summary

  • Register service with @Provide()
  • Keep business logic in service layer
  • Prefer async methods for consistency
Powered by WebContainers
Files
Preparing Environment
  • npm install
  • npm run dev