EggCore

EggCore

new EggCore(options)

Parameters:
Name Type Description
options Object

options

Properties
Name Type Description
[ baseDir = process.cwd() ] String

the directory of application

[ type = application|agent ] String

whether it's running in app worker or agent worker

[ plugins ] Object

custom plugins

Since:
  • 1.0.0
Source:

Members

BaseContextClass :BaseContextClass

Since:
  • 1.0.0
Source:

baseDir :String

The current directory of application

Since:
  • 1.0.0
Source:
See:

config :Config

The configuration of application

Since:
  • 1.0.0
Source:

Controller :Controller

Retrieve base controller

Since:
  • 1.0.0
Source:

deprecate :function

Since:
  • 1.0.0
Source:

loader :EggLoader

The loader instance, the default class is EggLoader. If you want define

Since:
  • 1.0.0
Source:

name :String

The name of application

Since:
  • 1.0.0
Source:
See:

plugins :Object

Retrieve enabled plugins

Since:
  • 1.0.0
Source:

router :Router

get router

Since:
  • 1.0.0
Source:

Service :Service

Retrieve base service

Since:
  • 1.0.0
Source:

type :String

Whether application or agent

Since:
  • 1.0.0
Source:

Methods

beforeClose(fn)

Register a function that will be called when app close.

Notice: This method is now NOT recommanded directly used, Developers SHOULDN'T use app.beforeClose directly now, but in the form of class to implement beforeClose instead.

Parameters:
Name Type Description
fn function

the function that can be generator function or async function.

Source:
See:

beforeStart(scope)

Execute scope after loaded and before app start.

Notice: This method is now NOT recommanded and reguarded as a deprecated one, For plugin development, we should use didLoad instead. For application development, we should use willReady instead.

Parameters:
Name Type Description
scope function | GeneratorFunction | AsyncFunction

function will execute before app start

Source:
See:

(async) close() → {Promise}

Close all, it will close

  • callbacks registered by beforeClose
  • emit close event
  • remove add listeners

If error is thrown when it's closing, the promise will reject. It will also reject after following call.

Since:
  • 1.0.0
Source:

ready(flagOrFunctionopt) → {Promise|null}

register an callback function that will be invoked when application is ready.

Parameters:
Name Type Description
[ flagOrFunction ] boolean | Error | function
Since:
  • 1.0.0
Source:
See:
Example
const app = new Application(...);
app.ready(err => {
  if (err) throw err;
  console.log('done');
});

readyCallback(name, opts) → {function}

If a client starts asynchronously, you can register readyCallback, then the application will wait for the callback to ready

It will log when the callback is not invoked after 10s

Recommend to use EggCore#beforeStart

Parameters:
Name Type Description
name String

readyCallback task name

opts object
  • {Number} [timeout=10000] - emit ready_timeout when it doesn't finish but reach the timeout
    • {Boolean} [isWeakDep=false] - whether it's a weak dependency
Since:
  • 1.0.0
Source:
Example
const done = app.readyCallback('mysql');
mysql.ready(done);

toAsyncFunction(fn) → {AsyncFunction}

Convert a generator function to a promisable one.

Notice: for other kinds of functions, it directly returns you what it is.

Parameters:
Name Type Description
fn function

The inputted function.

Source:
Example
```javascript
     const fn = function* (arg) {
        return arg;
      };
      const wrapped = app.toAsyncFunction(fn);
      wrapped(true).then((value) => console.log(value));
    ```

toPromise(obj) → {Promise}

Convert an object with generator functions to a Promisable one.

Parameters:
Name Type Description
obj Mixed

The inputted object.

Source:
Example
```javascript
     const fn = function* (arg) {
        return arg;
      };
      const arr = [ fn(1), fn(2) ];
      const promise = app.toPromise(arr);
      promise.then(res => console.log(res));
    ```

url(name, params) → {String}

Alias to Router#url

Parameters:
Name Type Description
name String

Router name

params Object

more parameters

Source:

use(fn) → {Application}

override koa's app.use, support generator function

Parameters:
Name Type Description
fn function

middleware

Since:
  • 1.0.0
Source: