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