代码风格指南

建议开发者使用 egg-init --type=simple showcase 来生成并观察推荐的项目结构和配置。

# Classify

// old style
module.exports = app => {
class UserService extends app.Service {
async list() {
return await this.ctx.curl('https://eggjs.org');
}
}
return UserService;
};

修改为:

const Service = require('egg').Service;
class UserService extends Service {
async list() {
return await this.ctx.curl('https://eggjs.org');
}
}
module.exports = UserService;

同时,框架开发者需要改变写法如下,否则应用开发者自定义 Service 等基类会有问题:

const egg = require('egg');

module.export = Object.assign(egg, {
Application: class MyApplication extends egg.Application {
// ...
},
// ...
});

# Private property && Lazy Initialization

// app/extend/application.js
const CACHE = Symbol('Application#cache');
const CacheManager = require('../../lib/cache_manager');

module.exports = {
get cache() {
if (!this[CACHE]) {
this[CACHE] = new CacheManager(this);
}
return this[CACHE];
},
}