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 = [ ];
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
ctxfor 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
Files
Preparing Environment
- npm install
- npm run dev