egg 項目實戰(五)Egg.js 中的Service服務


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

.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM