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 */
.