Extend EGG
Egg.js is extensible and it provides multiple extension points to enhance the functionality of itself:
- Application
- Context
- Request
- Response
- Helper
We could use the extension APIs to help us to develop, or extend the objects given above to enhance the functionality of egg as well while programming.
# Application
The object app
is just the same aspect as the global application object in Koa. There should be only one app
in your application, and it will be created by egg when the application is started.
# Access Method
-
ctx.app
-
You can access the Application object by using
this.app
in Controller, Middleware, Helper, Service. For instance,this.app.config
will help you access the Config object. -
The
app
object would be injected into the entry function as the first argument inapp.js
, like this:// app.js
module.exports = app => {
// here you can use the app object
};
# How to Extend
Egg will merge the object defined in app/extend/application.js
with the prototype of Application object in Koa, then generate object app
which is based on the extended prototype when application is started.
# Extend Methods
If we want to create a method app.foo()
, we can do it like this: :
// app/extend/application.js |
# Extend Properties
Generally speaking, the calculation of properties only need to be done once, therefore we have to do some cache, otherwise it will degrade performance of the app as too much calculation would be going to do when accessing those properties several times.
So, it's recommended to use Symbol + Getter.
For example, if we would like to add a Getter property app.bar
:
// app/extend/application.js |
# Context
Context means the context in Koa, which is a Request Level object. That is to say, every request from client will generate an Context instance. We usually write Context as ctx
in short. In all the doc, both Context and ctx
means the context object in Koa.
# Access Method
this
in middleware is ctx, such asthis.cookies.get('foo')
。- There are two different ways to write controller. If you use class to describe controller, you can use
this.ctx
to access Context. Or if you write as method, you can access Context withctx
directly. this
in helper, service points to the helper object and service object themselves. Simply usethis.ctx
to access Context object, such asthis.ctx.cookies.get('foo')
.
# How to extend
Egg will merge the object defined in app/extend/context.js
with the prototype of Context object in Koa. And it will generate a ctx
object which is based on the extended prototype when deal with request.
# Extend Methods
For instance, we could add a method ctx.foo()
in the following way:
// app/extend/context.js |
# Extend Properties
Generally speaking, the calculation of properties only need to do once, therefore we have to do some cache, otherwise it will degrade performance of the app as too much calculation would be going to do when access those properties several times.
So, it's recommended to use Symbol + Getter.
For example, if we would like to add a Getter property ctx.bar
:
// app/extend/context.js |
# Request
Request object is the same as that in Koa, which is a Request Level object. It provides a great number of methods to help to access the properties and methods you need.
# Access Method
ctx.request |
So many properties and methods in ctx
can also be accessed in request
object. For those properties and methods, it is just the same to access them by using either ctx
or request
, such as ctx.url === ctx.request.url
.
Here are the properties and methods in ctx
which can also be accessed by Request aliases: Koa - Request aliases
# How to Extend
Egg will merge the object defined in app/extend/request.js
and the prototype of request
object built in egg. And it will generate a request
object which is based on the extended prototype when deal with request.
For instance, we could add a property request.foo
in the following way:
// app/extend/request.js |
# Response
Response object is the same as that in Koa, which is a Request Level object. It provides a great number of methods to help to access the properties and methods you need.
# Access Method
ctx.response |
So many properties and methods in ctx
can also be accessed in response
object. For those properties and methods, it is just the same to access them by using either ctx
or response
. For example ctx.status = 404
is the same as ctx.response.status = 404
.
Here are the properties and methods in ctx
which can also be accessed by Response aliases: Koa Response aliases
# How to Extend
Egg will merge the object defined in app/extend/response.js
and the prototype of response
object build in egg. And it will generate a response
object which is based on the extended prototype after dealt with request.
For instance, we could add a setter request.foo
in the following way:
// app/extend/response.js |
Then we can use the setter like this: this.response.foo = 'bar';
# Helper
Function Helper can provides some useful utility functions.
We can put some utility functions we use ofter into helper.js as an individual function. Then we can write the complex codes in JavaScript, avoiding to write them everywhere. Besides, such a simple function like Helper allows to write test case much easier.
Egg has had some build-in Helper functions. We can write our own Helper as well.
# Access Method
Access helper object with ctx.helper
, for example:
// Assume that home router has already defined in app/router.js |
# How to Extend
Egg will merge the object defined in app/extend/helper.js
and the prototype of helper
object build in egg. And it will generate a helper
object which is based on the extended prototype after dealt with request.
For instance, we could add a method helper.foo()
in the following way:
// app/extend/helper.js |
# Extend according to environment
Besides, we can extend the framework in an optional way according to the environment. For example, if you want mockXX()
only be able to accessed when doing unittest:
// app/extend/application.unittest.js |
This file will only be required under unittest environment.
Similarly, we could extend egg in this way for other object,such as Application, Context, Request, Response and Helper. See more on environment