EggApplication

EggApplication

Base on koa's Application

Constructor

new EggApplication(options)

Parameters:
Name Type Description
options Object
  • {Object} [type] - type of instance, Agent and Application both extend koa, type can determine what it is.
    • {String} [baseDir] - app root dir, default is process.cwd()
    • {Object} [plugins] - custom plugin config, use it in unittest
Source:
See:

Extends

Members

BaseContextClass :BaseContextClass

Since:
  • 1.0.0
Overrides:
Source:

baseDir :String

The current directory of application

Since:
  • 1.0.0
Inherited From:
Source:
See:

config :Config

The configuration of application

Since:
  • 1.0.0
Inherited From:
Source:

Controller :Controller

Retrieve base controller

Since:
  • 1.0.0
Overrides:
Source:

coreLogger :Logger

core logger for framework and plugins, log file is $HOME/logs/{appname}/egg-web

Since:
  • 1.0.0
Source:

deprecate :function

Since:
  • 1.0.0
Inherited From:
Source:

env

app.env delegate app.config.env

Deprecated:
  • Yes
Source:

httpclient :HttpClient

HttpClient instance

Source:
See:

loader :EggLoader

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

Since:
  • 1.0.0
Inherited From:
Source:

logger :Logger

application logger, log file is $HOME/logs/{appname}/{appname}-web

Since:
  • 1.0.0
Source:

loggers :Object

All loggers contain logger, coreLogger and customLogger

Since:
  • 1.0.0
Source:

messenger :Messenger

messenger instance

Since:
  • 1.0.0
Source:

name :String

The name of application

Since:
  • 1.0.0
Inherited From:
Source:
See:

plugins :Object

Retrieve enabled plugins

Since:
  • 1.0.0
Inherited From:
Source:

proxy

app.proxy delegate app.config.proxy

Deprecated:
  • Yes
Source:

router :Router

get router

Since:
  • 1.0.0
Inherited From:
Source:

Service :Service

Retrieve base service

Since:
  • 1.0.0
Overrides:
Source:

type :String

Whether application or agent

Since:
  • 1.0.0
Inherited From:
Source:

Methods

addSingleton(name, create)

create a singleton instance

Parameters:
Name Type Description
name String

unique name for singleton

create Object

method will be invoked when singleton instance create

Source:

beforeClose(fn)

Register a function that will be called when app close

Parameters:
Name Type Description
fn function

the function that can be generator function or async function

Inherited From:
Source:

beforeStart(scope)

Execute scope after loaded and before app start

Parameters:
Name Type Description
scope function | GeneratorFunction | AsyncFunction

function will execute before app start

Inherited From:
Source:

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
Inherited From:
Source:

cluster(clientClass, optionsopt) → {ClientWrapper}

almost the same as Agent.cluster API, the only different is that this method create Follower.

Parameters:
Name Type Description
clientClass function

client class function

[ options ] Object
  • {Boolean} [autoGenerate] - whether generate delegate rule automatically, default is true
    • {Function} [formatKey] - a method to tranform the subscription info into a stringļ¼Œdefault is JSON.stringify
    • {Object} [transcode|JSON.stringify/parse]
      • {Function} encode - custom serialize method
      • {Function} decode - custom deserialize method
    • {Boolean} [isBroadcast] - whether broadcast subscrption result to all followers or just one, default is true
    • {Number} [responseTimeout] - response timeout, default is 3 seconds
    • {Number} [maxWaitTime|30000] - leader startup max time, default is 30 seconds
Source:
See:

curl(url, opts) → {Object}

http request helper base on httpclient, it will auto save httpclient log. Keep the same api with httpclient.request(url, args).

See https://github.com/node-modules/urllib#api-doc for more details.

Parameters:
Name Type Description
url String

request url address.

opts Object
  • method {String} - Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'.
  • data {Object} - Data to be sent. Will be stringify automatically.
  • dataType {String} - String - Type of response data. Could be text or json. If it's text, the callbacked data would be a String. If it's json, the data of callback would be a parsed JSON Object. Default callbacked data would be a Buffer.
  • headers {Object} - Request headers.
  • timeout {Number} - Request timeout in milliseconds. Defaults to exports.TIMEOUT. Include remote server connecting timeout and response timeout. When timeout happen, will return ConnectionTimeout or ResponseTimeout.
  • auth {String} - username:password used in HTTP Basic Authorization.
  • followRedirect {Boolean} - follow HTTP 3xx responses as redirects. defaults to false.
  • gzip {Boolean} - let you get the res object when request connected, default false. alias customResponse
  • nestedQuerystring {Boolean} - urllib default use querystring to stringify form data which don't support nested object, will use qs instead of querystring to support nested object by set this option to true.
  • more options see https://www.npmjs.com/package/urllib
Source:
Example
```js
const result = yield app.curl('http://example.com/foo.json', {
  method: 'GET',
  dataType: 'json',
});
console.log(result.status, result.headers, result.data);
```

getLogger(name) → {Logger}

Get logger by name, it's equal to app.loggers['name'], but you can extend it with your own logical.

Parameters:
Name Type Description
name String

logger name

Source:

inspect() → {Object}

print the infomation when console.log(app)

Since:
  • 1.0.0
Source:
Example
```js
console.log(app);
=>
{
  name: 'mockapp',
  env: 'test',
  subdomainOffset: 2,
  config: '<egg config>',
  controller: '<egg controller>',
  service: '<egg service>',
  middlewares: '<egg middlewares>',
  urllib: '<egg urllib>',
  loggers: '<egg loggers>'
}
```

url(name, params) → {String}

Alias to Router#url

Parameters:
Name Type Description
name String

Router name

params Object

more parameters

Inherited From:
Source:

use(fn) → {Application}

override koa's app.use, support async function

Parameters:
Name Type Description
fn function

middleware

Since:
  • 1.0.0
Inherited From:
Source: