1.创建service文件
app/service/product.js
const Service = require('egg').Service;
class ProductService extends Service {
async index() {
return {
id: 100,
name: '测试'
}
}
}
module.exports = ProductService;
2.调用
async index() {
const { ctx } = this;
const res = await ctx.service.product.index();
ctx.body = res;
}
3.服务的命名规则
/*** * 服务的命名规则 * Service 文件必须放在 app/service 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。 * app/service/biz/user.js => ctx.service.biz.user (推荐) * app/service/sync_user.js => ctx.service.syncUser * app/service/HackerNews.js => ctx.service.hackerNews */
.