Constructor
new EggApplication(options)
Parameters:
Name | Type | Description |
---|---|---|
options
|
Object |
|
Extends
Members
BaseContextClass :BaseContextClass
- Since:
- 1.0.0
- Overrides:
- Source:
baseDir :String
The current directory of application
- Since:
- 1.0.0
- Overrides:
- Source:
- See:
Boot :Boot
Retreive base boot
- Source:
config :Config
The configuration of application
- Since:
- 1.0.0
- Overrides:
- 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:
createAnonymousContext :String
Create an anonymous context, the context isn't request level, so the request is mocked.
then you can use context level API like ctx.service
- Source:
deprecate :function
Alias to https://npmjs.com/package/depd
- Since:
- 1.0.0
- Overrides:
- 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
- Overrides:
- 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
- Overrides:
- Source:
- See:
plugins :Object
Retrieve enabled plugins
- Since:
- 1.0.0
- Overrides:
- Source:
proxy
app.proxy delegate app.config.proxy
- Deprecated:
- Yes
- Source:
router :Router
get router
- Since:
- 1.0.0
- Overrides:
- Source:
Service :Service
Retrieve base service
- Since:
- 1.0.0
- Overrides:
- Source:
type :String
Whether application
or agent
- Since:
- 1.0.0
- Overrides:
- Source:
Methods
addSingleton(name, create)
create a singleton instance
Parameters:
Name | Type | Description |
---|---|---|
name
|
String | unique name for singleton |
create
|
function | AsyncFunction | method will be invoked when singleton instance create |
- Source:
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. |
- Overrides:
- 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 |
- Overrides:
- 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
- Overrides:
- 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 |
|
- Source:
- See:
createContext(req, res) → {Context}
Create egg context
Parameters:
Name | Type | Description |
---|---|---|
req
|
Req | node native Request object |
res
|
Res | node native Response object |
- Source:
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 |
|
- Source:
Example
```js
const result = await 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 information 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>'
}
```
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
- Overrides:
- 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 |
|
- Since:
- 1.0.0
- Overrides:
- 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. |
- Overrides:
- 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. |
- Overrides:
- 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 |
- Overrides:
- Source:
use(fn) → {Application}
override koa's app.use, support generator function
Parameters:
Name | Type | Description |
---|---|---|
fn
|
function | middleware |
- Since:
- 1.0.0
- Overrides:
- Source: