5.4.1 • Published 4 months ago

egg-core v5.4.1

Weekly downloads
23,287
License
MIT
Repository
github
Last release
4 months ago

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.

serverEnvdescription
defaultdefault environment
testsystem integration testing environment
prodproduction environment
locallocal environment on your own computer
unittestunit 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

ParamTypeDescription
directoryString/Arraydirectories to be loaded
targetObjectattach the target object from loaded files
matchString/Arraymatch the files when load, default to **/*.js(if process.env.EGG_TYPESCRIPT was true, default to [ '**/*.(js|ts)', '!**/*.d.ts' ])
ignoreString/Arrayignore the files when load
initializerFunctioncustom 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
caseStyleString/Functionset property's case when converting a filepath to property list.
overrideBooleandetermine whether override the property when get the same name
callBooleandetermine whether invoke when exports is function
injectObjectan object that be the argument when invoke the function
filterFunctiona 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

popomoredead-horsefengmk2atian25whxaxesgxcsoccer
killaguiyuqngotinitial-wuwaitingsongAnzerWall
army8735njugrayJacksonTianJimmyDaddyXadillaXmonkindey
mattmashaoshuai0102zhang740dsonetchenbin92hyj1991
maxming2333supperchongZhangDianPengmosaic101

This project follows the git-contributor spec, auto updated at Wed Nov 24 2021 22:24:39 GMT+0800.

4.31.0

4 months ago

5.4.1

4 months ago

5.4.0

4 months ago

5.3.1

1 year ago

4.30.2

1 year ago

5.3.0

1 year ago

5.0.0

1 year ago

4.29.0

1 year ago

5.1.1

1 year ago

5.1.0

1 year ago

4.28.0

1 year ago

4.28.1

1 year ago

5.2.0

1 year ago

4.27.0

2 years ago

4.30.1

1 year ago

4.30.0

1 year ago

4.25.0

2 years ago

4.26.0

2 years ago

4.26.1

2 years ago

4.24.0

2 years ago

4.24.1

2 years ago

4.23.0

2 years ago

4.22.0

2 years ago

4.22.1

2 years ago

4.21.0

2 years ago

4.20.0

4 years ago

4.19.1

4 years ago

4.19.0

4 years ago

4.18.0

4 years ago

4.17.6

4 years ago

4.17.5

4 years ago

4.17.4

4 years ago

4.17.3

5 years ago

4.17.2

5 years ago

3.21.4

5 years ago

4.17.1

5 years ago

4.17.0

5 years ago

4.16.2

5 years ago

4.16.1

5 years ago

4.16.0

5 years ago

4.15.0

5 years ago

4.14.1

5 years ago

4.14.0

5 years ago

4.13.3

5 years ago

4.13.2

5 years ago

4.13.1

5 years ago

4.13.0

5 years ago

4.12.0

5 years ago

4.11.0

6 years ago

4.10.3

6 years ago

4.10.2

6 years ago

4.10.1

6 years ago

4.10.0

6 years ago

4.9.1

6 years ago

4.9.0

6 years ago

4.8.0

6 years ago

3.21.3

6 years ago

3.21.2

6 years ago

3.21.1

6 years ago

3.21.0

6 years ago

4.7.1

6 years ago

4.7.0

6 years ago

4.6.0

6 years ago

4.5.0

6 years ago

3.20.3

6 years ago

4.4.1

6 years ago

4.4.0

6 years ago

4.3.2

6 years ago

4.3.1

6 years ago

3.20.2

6 years ago

3.20.1

6 years ago

3.20.0

6 years ago

4.3.0

6 years ago

3.19.3

6 years ago

4.2.2

6 years ago

4.2.1

6 years ago

3.19.2

6 years ago

3.19.1

6 years ago

4.2.0

6 years ago

3.19.0

6 years ago

4.1.0

6 years ago

4.0.0

6 years ago

3.18.0

6 years ago

3.17.0

6 years ago

3.16.0

6 years ago

3.15.1

7 years ago

3.15.0

7 years ago

3.14.0

7 years ago

3.13.1

7 years ago

3.13.0

7 years ago

3.12.2

7 years ago

3.12.1

7 years ago

3.12.0

7 years ago

3.11.0

7 years ago

3.10.0

7 years ago

3.9.0

7 years ago

3.8.0

7 years ago

3.7.0

7 years ago

3.6.0

7 years ago

3.5.0

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.3.1

7 years ago

3.3.0

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.8.0

7 years ago

1.7.0

7 years ago

1.6.0

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago