包详细信息

egg-core

eggjs98.2kMIT5.5.1

A core Pluggable framework based on koa

egg, loader

自述文件

egg-core

NPM version Node.js CI Test coverage Known Vulnerabilities npm download

A core Pluggable framework based on koa.

Don't use it directly, see egg.

Usage

Directory structure

├── package.json
├── app.js (optional)
├── agent.js (optional)
├── app
|   ├── router.js
│   ├── controller
│   │   └── home.js
|   ├── extend (optional)
│   |   ├── helper.js (optional)
│   |   ├── filter.js (optional)
│   |   ├── request.js (optional)
│   |   ├── response.js (optional)
│   |   ├── context.js (optional)
│   |   ├── application.js (optional)
│   |   └── agent.js (optional)
│   ├── service (optional)
│   ├── middleware (optional)
│   │   └── response_time.js
│   └── view (optional)
|       ├── layout.html
│       └── home.html
├── config
|   ├── config.default.js
│   ├── config.prod.js
|   ├── config.test.js (optional)
|   ├── config.local.js (optional)
|   ├── config.unittest.js (optional)
│   └── plugin.js

Then you can start with code below

const Application = require('egg-core').EggCore;
const app = new Application({
  baseDir: '/path/to/app'
});
app.ready(() => app.listen(3000));

EggLoader

EggLoader can easily load files or directories in your [egg] project. In addition, you can customize the loader with low level APIs.

constructor

  • {String} baseDir - current directory of application
  • {Object} app - instance of egg application
  • {Object} plugins - merge plugins for test
  • {Logger} logger - logger instance,default is console

High Level APIs

loadPlugin

Load config/plugin.js

loadConfig

Load config/config.js and config/{serverEnv}.js

If process.env.EGG_APP_CONFIG is exists, then it will be parse and override config.

loadController

Load app/controller

loadMiddleware

Load app/middleware

loadApplicationExtend

Load app/extend/application.js

loadContextExtend

Load app/extend/context.js

loadRequestExtend

Load app/extend/request.js

loadResponseExtend

Load app/extend/response.js

loadHelperExtend

Load app/extend/helper.js

loadCustomApp

Load app.js, if app.js export boot class, then trigger configDidLoad

loadCustomAgent

Load agent.js, if agent.js export boot class, then trigger configDidLoad

loadService

Load app/service

Low Level APIs

getServerEnv()

Retrieve application environment variable values via serverEnv. You can access directly by calling this.serverEnv after instantiation.

serverEnv description
default default environment
test system integration testing environment
prod production environment
local local environment on your own computer
unittest unit test environment

getEggPaths()

To get directories of the frameworks. A new framework is created by extending egg, then you can use this function to get all frameworks.

getLoadUnits()

A loadUnit is a directory that can be loaded by EggLoader, cause it has the same structure.

This function will get add loadUnits follow the order:

  1. plugin
  2. framework
  3. app

loadUnit has a path and a type. Type must be one of those values: app, framework, plugin.

{
  path: 'path/to/application',
  type: 'app'
}

getAppname()

To get application name from package.json

appInfo

Get the infomation of the application

  • pkg: package.json
  • name: the application name from package.json
  • baseDir: current directory of application
  • env: equals to serverEnv
  • HOME: home directory of the OS
  • root: baseDir when local and unittest, HOME when other environment

loadFile(filepath)

To load a single file. Note: The file must export as a function.

loadToApp(directory, property, LoaderOptions)

To load files from directory in the application.

Invoke this.loadToApp('$baseDir/app/controller', 'controller'), then you can use it by app.controller.

loadToContext(directory, property, LoaderOptions)

To load files from directory, and it will be bound the context.

// define service in app/service/query.js
module.exports = class Query {
  constructor(ctx) {
    super(ctx);
    // get the ctx
  }

  async get() {}
};

// use the service in app/controller/home.js
module.exports = async ctx => {
  ctx.body = await ctx.service.query.get();
};

loadExtend(name, target)

Loader app/extend/xx.js to target, For example,

this.loadExtend('application', app);

LoaderOptions

Param Type Description
directory String/Array directories to be loaded
target Object attach the target object from loaded files
match String/Array match the files when load, default to **/*.js(if process.env.EGG_TYPESCRIPT was true, default to `[ '*/.(js ts)', '!*/.d.ts' ]`)
ignore String/Array ignore the files when load
initializer Function custom file exports, receive two parameters, first is the inject object(if not js file, will be content buffer), second is an options object that contain path
caseStyle String/Function set property's case when converting a filepath to property list.
override Boolean determine whether override the property when get the same name
call Boolean determine whether invoke when exports is function
inject Object an object that be the argument when invoke the function
filter Function a function that filter the exports which can be loaded

Timing

EggCore record boot progress with Timing, include:

  • Process start time
  • Script start time(node don't implement an interface like process.uptime to record the script start running time, framework can implement a prestart file used with node --require options to set process.scriptTime)
  • Application start time
  • Load duration
  • require duration

start

Start record a item. If the item exits, end the old one and start a new one.

  • {String} name - record item name
  • {Number} [start] - record item start time, default is Date.now()

end

End a item.

  • {String} name - end item name

toJSON

Generate all record items to json

  • {String} name - record item name
  • {Number} start - item start time
  • {Number} end - item end time
  • {Number} duration - item duration
  • {Number} pid - pid
  • {Number} index - item index

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

更新日志

Changelog

5.5.1 (2025-01-22)

Bug Fixes

  • support EGG_OPTIONS_PATH_TO_REGEXP_MODULE env (#292) (f253ea2)

5.5.0 (2025-01-22)

Features

  • support custom pathToRegexpModule on app.options (#290) (72ba3a8)

5.4.1 / 2023-12-20

fixes

5.4.0 / 2023-12-19

features

others

5.3.1 (2023-04-06)

Bug Fixes

5.3.0 (2023-01-13)

Features

5.2.0 (2023-01-03)

Features

5.1.1 (2023-01-01)

Bug Fixes

  • @types/depd and @types/koa should be dependencies (#257) (91937c3)

5.1.0 (2023-01-01)

Features

5.0.0 (2023-01-01)

⚠ BREAKING CHANGES

  • drop Node.js < 14.17.0 support

Features

  • upgrade dependencies and devDependencies (#256) (540f3fe)

4.30.1 (2022-12-19)

Bug Fixes

  • should support pkg.egg.typescript = true config (#255) (85f2eda)

4.30.0 (2022-12-19)

Features

  • auto register tsconfig-paths on env.EGG_TYPESCRIPT enable (#254) (6d75322)

4.29.0 / 2022-12-06

features

4.28.1 / 2022-11-28

fixes

4.28.0 / 2022-11-28

features

4.27.0 / 2022-10-14

features

  • [7750ebc] - feat: dump plugin info when it implicit enable by dependents plugin (#248) (TZ | 天猪 <atian25@qq.com>)

4.26.1 / 2022-09-20

fixes

4.26.0 / 2022-09-07

features

4.25.0 / 2022-09-07

features

others

4.24.1 / 2022-06-23

fixes

4.24.0 / 2022-06-21

others

4.23.0 / 2022-02-10

features

4.22.1 / 2022-01-28

fixes

4.22.0 / 2022-01-07

features

4.21.0 / 2021-11-24

others

4.20.0 / 2020-09-23

features

4.19.1 / 2020-09-15

fixes

4.19.0 / 2020-09-11

features

4.18.0 / 2020-08-18

features

4.17.6 / 2020-08-05

fixes

4.17.5 / 2020-08-04

others

4.17.4 / 2019-12-11

fixes

4.17.3 / 2019-07-07

fixes

others

4.17.2 / 2019-05-14

4.17.1 / 2019-04-24

fixes

4.17.0 / 2019-04-24

features

4.16.2 / 2019-04-11

others

4.16.1 / 2019-03-20

fixes

4.16.0 / 2019-03-19

features

fixes

4.15.0 / 2019-03-06

features

fixes

4.14.1 / 2019-02-15

others

4.14.0 / 2019-02-03

features

4.13.3 / 2019-01-30

others

4.13.2 / 2019-01-30

others

4.13.1 / 2019-01-11

others

4.13.0 / 2018-12-14

features

4.12.0 / 2018-12-11

features

others

4.11.0 / 2018-10-19

features

4.10.3 / 2018-09-29

fixes

  • [58a49e4] - fix(lifecycle): forbid adding hook after initialization (#184) (initialwu)

others

  • [9c16f2e] - chore(eslint): set root=true to stop looking in parent folders (#183) (initialwu)

4.10.2 / 2018-09-21

fixes

4.10.1 / 2018-09-21

fixes

4.10.0 / 2018-09-06

features

others

4.9.1 / 2018-07-12

  • revert: #172 loadUnit.name (#175)
  • chore(typings): add pkg.types and pkg.files entry for index.d.ts (#176)

4.9.0 / 2018-07-09

  • chore(typings): add index.d.ts (#169)
  • feat: loadUnit should exports name (#172)
  • fix: remove useless code (#170)
  • docs: fix a typo (#168)

4.8.0 / 2018-05-22

features

others

4.7.1 / 2018-04-25

fixes

  • [4508c36] - fix: toAsyncFunction can't pass is.asyncFunction() (#159) (Khaidi Chu <i@2333.moe>)

4.7.0 / 2018-04-21

  • feat: support ts by env (#158)

4.6.0 / 2018-04-09

features

4.5.0 / 2018-03-25

features

4.4.1 / 2018-03-09

fixes

4.4.0 / 2018-01-18

features

4.3.2 / 2018-01-13

fixes

4.3.1 / 2018-01-12

fixes

others

4.3.0 / 2017-12-13

features

4.2.2 / 2017-12-12

fixes

4.2.1 / 2017-12-01

fixes

4.2.0 / 2017-11-29

features

4.1.0 / 2017-11-20

features

others

4.0.0 / 2017-11-08

others

3.18.0 / 2017-11-08

features

3.17.0 / 2017-11-07

features

3.16.0 / 2017-11-06

features

3.15.1 / 2017-10-29

others

3.15.0 / 2017-10-20

features

3.14.0 / 2017-10-17

features

  • [c2dec90] - feat(core): adding support to register inherited methods when loading controllers (#119) (lkspc <lkspc@qq.com>)

3.13.1 / 2017-09-01

  • fix: TypeError when DEBUG=* (#112)

3.13.0 / 2017-07-24

  • feat: controller support params by config (#110)
  • style: spelling mistakes,orginal -> original (#109)

3.12.2 / 2017-07-11

  • fix: check loader existing before retrieve properties (#108)

3.12.1 / 2017-07-05

  • fix: should ignore Object.getPrototypeOf check on null/undefined (#107)

3.12.0 / 2017-07-05

  • feat: generate configMeta (#106)
  • deps: upgrade eslint (#104)
  • docs: fix typo (#103)
  • deps: upgrade dependencies (#102)
  • refactor(plugin): ignore loop when push plugin.default.js (#101)

3.11.0 / 2017-06-21

  • feat: framework can override getExtendFilePaths (#100)

3.10.0 / 2017-06-08

  • chore: improve cov (#91)
  • feat: support app.middleware[name] (#98)
  • test: add node 8 (#97)

3.9.0 / 2017-05-31

  • feat: app timeout support config by env (#94)
  • fix: load class controller should skip getter & setter (#96)
  • refactor: use template literals in lib/utils/index.js (#95)

3.8.0 / 2017-05-20

  • feat: support load custom file type (#93)
  • chore(documentation): fix typo (#92)
  • test: fix the testcase that is skipped (#89)
  • refactor: change private function name to Symbol from being called outside. (#87)
  • test: skip the failed testcase (#88)
  • refactor: use es6 rest parameter. (#84)

3.7.0 / 2017-05-03

  • feat(file_loader): support filter options (#86)
  • feat: support custom directory (#85)
  • refact: use es6 default parameter value synax. (#83)

3.6.0 / 2017-05-02

  • feat: add fullPath property on class instance (#82)

3.5.0 / 2017-04-26

  • feat(file_loader): ignore option support array in FileLoader (#81)
  • fix: wrong optional dependencies in complex demo (#80)

3.4.1 / 2017-04-21

  • fix: should support module.exports = function*(ctx) {} as a controller (#79)

3.4.0 / 2017-04-18

  • refactor: export getHomedir that can be extended (#78)
  • feat: expose eggPlugins (#77)

3.3.1 / 2017-04-17

  • fix: optionally depend on a plugin which is disabled. (#76)

3.3.0 / 2017-04-15

  • feat: always load extend/xx.unittest.js when run test (#75)

3.2.2 / 2017-04-14

  • fix: don't replace plugin.default.js when serverEnv is default (#74)

3.2.1 / 2017-04-13

  • fix: allow extend setter or getter alone (#73)

3.2.0 / 2017-04-11

  • test: add testcase for appPlugins and customPlugins (#72)
  • fix: find the true callee bebind proxy (#70)
  • feat:expose appPlugins & customPlugins (#68)
  • feat: expose BaseContextClass (#71)

3.1.0 / 2017-04-10

  • feat: to keep controller function attributes (#69)

3.0.1 / 2017-04-10

  • fix: ensure deprecate display the right call stack (#67)

3.0.0 / 2017-03-07

  • feat: [BREAKING_CHANGE] array will be overridden when load config (#64)

2.2.0 / 2017-02-27

  • fix: improve getPathName (#62)
  • feat: FileLoader support caseStyle (#59)
  • fix: improve require es module (#61)

2.1.1 / 2017-02-17

  • fix: define egg.Service and egg.Controller in constructor (#58)

2.1.0 / 2017-02-15

  • feat: load plugin.default.js rather than plugin.js (#57)
  • refactor: seperate router api from app (#55)

2.0.1 / 2017-02-15

  • fix: context loader cache independent in each request (#54)

2.0.0 / 2017-02-10

  • feat: [BREAKING_CHANGE] can get error from .ready() (#53)
  • fix: make sure close once (#51)
  • feat: imporve error message of async controller (#52)
  • deps: remove unuse devDeps (#49)
  • feat: [BREAKING_CHANGE] all middleware support async function and common function (#50)

1.8.0 / 2017-02-06

  • feat: app.beforeStart support async function same as beforeClose (#48)
  • test: fix test on windows (#47)
  • feat: add this.service in BaseContextClass (#46)
  • feat: add this.config in BaseContextClass (#44)
  • fix: execute beforeClose hooks in reverse order (#45)

1.7.0 / 2017-01-26

  • feat: add app.beforeClose to register close function (#43)

1.6.0 / 2017-01-20

  • feat: controller support class (#42)

1.5.1 / 2017-01-19

  • fix: don't assert config.proxy (#41)

1.5.0 / 2017-01-17

  • feat: plugin support optionalDependencies (#40)

1.4.0 / 2017-01-12

  • refactor: support config/env instead of config/serverEnv (#37)
  • fix(router): support app.get(url, controllerName) (#38)
  • feat: support app.beforeStart (#39)

1.3.3 / 2016-12-28

  • test: use assert instead of should
  • refactor: warn only for redefine the same package

1.3.2 / 2016-12-08

  • fix: distinguish property cache (#35)

1.3.1 / 2016-12-03

  • fix: router.url can't parse multi params right (#34)

1.3.0 / 2016-11-25

  • feat: make app middlewares also support enable (#33)

1.2.0 / 2016-11-21

  • refactor: don't use core middleware when enable = false (#32)
  • feat: core middlewares support enable/match/ignore options (#31)

1.1.0 / 2016-11-09

  • refactor: extract getAppInfo that can be extend (#30)

1.0.1 / 2016-11-07

  • chore: add pkg.files (#29)

1.0.0 / 2016-11-04

  • feat: warn when redefine plugin (#28)
  • refactor: assert eggPath should be string

0.6.0 / 2016-10-28

  • feat: app support export generator (#26)

0.5.0 / 2016-10-24

  • feat: app.js/agent.js support async function (#18)
  • feat: add EGG_HOME to getHomedir for test (#25)

0.4.0 / 2016-10-24

  • feat: support plugin.{env}.js (#20)
  • feat: support {env}.js when load extend (#21)
  • feat: app.close return a promise (#19)
  • feat: [BREAKING_CHANGE] env as prod when EGG_SERVER_ENV undefined & NODE_ENV prod (#24)
  • feat: warning when missing EGG_SERVER_ENV at production (#23)
  • test: fix homedir testcase on Windows (#22)

0.3.0 / 2016-10-13

  • fix: always get the executor's homedir (#17)
  • doc: Plugable > Pluggable (#16)
  • test: delete type testcase (#15)
  • fix: can't get appConfig in appConfig (#14)
  • feat: add plugin.from where declare the plugin (#13)
  • feat: [BREAKING_CHANGE] remove compatible support loadExtend (#12)

0.2.1 / 2016-08-18

  • fix: resolve the realpath of plugin path (#11)

0.2.0 / 2016-08-17

  • feat: improve initializer && export Loader

0.1.0 / 2016-08-16

  • feat: rename egg-loader to egg-core (#8)
  • refactor: rename to egg-core (#6)
  • doc: proofread readme documentation and correct english terms (#7)
  • refactor API (#5)
  • refactor: implement Loader instead of loading (#4)

0.0.3 / 2016-07-30

  • test: add testcase (#3)
  • fix: don't print middleware options on start log (#2)

0.0.2 / 2016-07-16

  • first version