site/docs/basics/service.md
⚠️ Translation in Progress: This page is being translated. For now, please refer to the Chinese version.
Service is an abstraction layer for encapsulating business logic in complex business scenarios. It provides the following advantages:
// app/service/user.js
const Service = require('egg').Service;
class UserService extends Service {
async find(uid) {
const user = await this.ctx.db.query(
'select * from user where uid = ?',
uid,
);
return user;
}
}
module.exports = UserService;
Each Service instance has access to:
this.ctx: Current request Context objectthis.app: Current Application objectthis.service: Access to other Servicesthis.config: Application configurationthis.logger: Logger object for debuggingFor more details, please refer to the Chinese documentation.