title: Framework Built-in Objects

At this chapter, we will introduce some built-in basic objects in the framework, including four objects (Application, Context, Request, Response) inherited from Koa and some objects that extended by the framework (Controller, Service , Helper, Config, Logger), we will often see them in the follow-up documents.

Application

Application is a global application object, an application only instantiates one Application, it is inherited from Koa.Application, we can mount some global methods and objects on it. We can easily [extend Application object] (./extend.md#Application) in plugin or application.

Events

Framework will emits some events when server running, application developers or plugin developers can listen on these events to do some job like logging. As application developers, we can listen on these events in app start script.

  • server: every worker will only emit once during the runtime, after HTTP server started, framework will expose HTTP server instance by this event.
  • error: if any exception catched by onerror plugin, it will emit an error event with the exception instance and current context instance(if have), developers can listen on this event to report or logging.
  • request and response: application will emit request and response event when receive requests and ended responses, developers can listen on these events to generate some digest log.
  1. // app.js
  2. module.exports = app => {
  3. app.once('server', server => {
  4. // websocket
  5. });
  6. app.on('error', (err, ctx) => {
  7. // report error
  8. });
  9. app.on('request', ctx => {
  10. // log receive request
  11. });
  12. app.on('response', ctx => {
  13. // ctx.starttime is set by framework
  14. const used = Date.now() - ctx.starttime;
  15. // log total cost
  16. });
  17. };

How to Get

Application object can be accessed almost anywhere in application, here are a few commonly used access ways:

Almost all files (Controller, Service, Schedule, etc.) loaded by the [Loader] (../advanced/loader.md) can export a function that is called by the Loader and uses the app as a parameter:

  • App start script

    1. // app.js
    2. module.exports = app => {
    3. app.cache = new Cache();
    4. };
  • Controller file

    1. // app/controller/user.js
    2. class UserController extends Controller {
    3. async fetch() {
    4. this.ctx.body = app.cache.get(this.ctx.query.id);
    5. }
    6. }

Like the Koa, on the Context object, we can access the Application object via ctx.app. Use the above Controller file as an example:

  1. // app/controller/user.js
  2. class UserController extends Controller {
  3. async fetch() {
  4. this.ctx.body = this.ctx.app.cache.get(this.ctx.query.id);
  5. }
  6. }

In the instance objects that inherited from the Controller and Service base classes, the Application object can be accessed via this.app.

  1. // app/controller/user.js
  2. class UserController extends Controller {
  3. async fetch() {
  4. this.ctx.body = this.app.cache.get(this.ctx.query.id);
  5. }
  6. };

Context

Context is a request level object, inherited from Koa.Context. When a request is received every time, the framework instantiates a Context object that encapsulates the information requested by the user and provides a number of convenient ways to get the request parameter or set the response information. The framework will mount all of the Service on the Context instance, and some plugins will mount some other methods and objects on it (egg-sequelize will mount all the models on the Context).

How to Get

The most common way to get the Context instance is in Middleware, Controller, and Service. The access method in the Controller is shown in the above example. In the Service, the access way is same as Controller. The access Context method in the Middleware of Egg is same as Koa framework gets the Context object in its middleware.

The Middleware of Egg also supports Koa v1 and Koa v2 two different middleware coding formats. use different format, the way to access Context instance is also slightly different:

  1. // Koa v1
  2. function* middleware(next) {
  3. // this is instance of Context
  4. console.log(this.query);
  5. yield next;
  6. }
  7. // Koa v2
  8. async function middleware(ctx, next) {
  9. // ctx is instance of Context
  10. console.log(ctx.query);
  11. }

In addition to the request can get the Context instance, in some non-request scenario we need to access service / model and other objects on the Context instance, we can useApplication.createAnonymousContext () method to create an anonymous Context instance:

  1. // app.js
  2. module.exports = app => {
  3. app.beforeStart(async () => {
  4. const ctx = app.createAnonymousContext();
  5. // preload before app start
  6. await ctx.service.posts.load();
  7. });
  8. }

Each task in Schedule takes a Context instance as a parameter so that we can more easily execute some schedule business logic:

  1. // app/schedule/refresh.js
  2. exports.task = async ctx => {
  3. await ctx.service.posts.refresh();
  4. };

Request & Response

Request is a request level object, inherited from Koa.Request. Encapsulates the Node.js native HTTP Request object, and provides a set of helper methods to get commonly used parameters of HTTP requests.

Response is a request level object, inherited from Koa.Response. Encapsulates the Node.js native HTTP Response object, and provides a set of helper methods to set the HTTP response.

How to Get

We can get the Request(ctx.request) and Response (ctx.response) instances of the current request on the Context instance.

  1. // app/controller/user.js
  2. class UserController extends Controller {
  3. async fetch() {
  4. const { app, ctx } = this;
  5. const id = ctx.request.query.id;
  6. ctx.response.body = app.cache.get(id);
  7. }
  8. }
  • Koa will proxy some methods and properties of Request and Response on Context, see Koa.Context.
  • ctx.request.query.id andctx.query.id are equivalent in the above example , ctx.response.body = and ctx.body = are equivalent.
  • It should be noted that get the body of POST should use ctx.request.body instead ofctx.body.

Controller

Egg provides a Controller base class and recommends that all Controller inherit from the base class. The Controller base class has the following properties:

  • ctx - Context instance of the current request.
  • app - Application instance.
  • config - application configuration.
  • service - all service of application.
  • logger - the encapsulated logger object for the current controller.

In the Controller file, there are two ways to use the Controller base class:

  1. // app/controller/user.js
  2. // get from egg (recommend)
  3. const Controller = require('egg').Controller;
  4. class UserController extends Controller {
  5. // implement
  6. }
  7. module.exports = UserController;
  8. // get from app instance
  9. module.exports = app => {
  10. return class UserController extends app.Controller {
  11. // implement
  12. };
  13. };

Service

Egg provides a Service base class and recommends that all Service inherit from the base class.

The properties of the Service base class are the same as those of the Controller base class, the access method is similar:

  1. // app/service/user.js
  2. // get from egg (recommend)
  3. const Service = require('egg').Service;
  4. class UserService extends Service {
  5. // implement
  6. }
  7. module.exports = UserService;
  8. // get from app instance
  9. module.exports = app => {
  10. return class UserService extends app.Service {
  11. // implement
  12. };
  13. };

Helper

Helper is used to provide some useful utility functions. Its role is that we can put some commonly used functions into helper.js, so we can use JavaScript to write complex logic, avoid the logic being scattered everywhere, and can be more convenient to write test cases.

The Helper itself is a class that has the same properties as the Controller base class, and it will be instantiated at each request so that all functions on the Helper can also get context of the current request.

How to Get

We can get the Helper(ctx.helper) instance of the current request on the Context instance.

  1. // app/controller/user.js
  2. class UserController extends Controller {
  3. async fetch() {
  4. const { app, ctx } = this;
  5. const id = ctx.query.id;
  6. const user = app.cache.get(id);
  7. ctx.body = ctx.helper.formatUser(user);
  8. }
  9. }

In addition, Helper instances can also be accessed in template, for example, we can get shtml method provided by security plugin in template.

  1. // app/view/home.nj
  2. {{ helper.shtml(value) }}

Custom helper method

In application development, we may often customize some helper methods, such as formatUser in the above example, we can customize helper method with a way of framework extension.

  1. // app/extend/helper.js
  2. module.exports = {
  3. formatUser(user) {
  4. return only(user, [ 'name', 'phone' ]);
  5. }
  6. };

Config

We recommend application development to follow the principle of configuration and code separation, put hard-coded business in configuration file, and the configuration file supports different runtime environments using different configurations, it is very convenient to use it. the framework, plugin and application-level configurations are available via the Config object. For configuration of Egg, read Configuration in detail.

How to Get

We can get the config object from the Application instance via app.config, or get the config object viathis.config on the instance of Controller, Service or Helper.

Logger

Egg builds in powerful logger, it is very convenient to print a variety of levels of logs to the corresponding log file, each logger object provides 5 level methods:

  • logger.debug()
  • logger.info()
  • logger.warn()
  • logger.error()

Egg provides a number of Logger object, we simply introduce how to get each Logger and its use scenario.

App Logger

We can get it via app.logger. If we want to do some application-level logging, such as logging some data in the startup phase, logging some business informations that are unrelated to request, those can be done by App Logger.

App CoreLogger

We can get it via app.coreLogger, and we should not print logs via CoreLogger when developing applications. the framework and plugins need to print application-level logs to make it easier to distinguish from logs printed by applications and logs printed by frameworks, the logs printed by the CoreLogger will be placed in a different file than the Logger.

Context Logger

We can get it via ctx.logger from Context instance, we can see from access method, Context Logger must be related to the request, it will take current request related information (such as [$userId/$ip/$traceId/${cost}ms $method $url]) in the front of logs, with this information, we can quickly locate requests from logs and concatenate all logs in one request.

Context CoreLogger

We can get it via ctx.coreLogger, the difference between the Context Logger is that only plugins and framework will log via it.

Controller Logger & Service Logger

We can get them via this.logger in Controller and Service instance, they are essentially a Context Logger, but additional file path will be added to logs, easy to locate the log print location.

Subscription

Subscription is a common model for subscribing, for example, the consumer in message broker or schedule, so we provide the Subscription base class to normalize this model.

The base class of Subscription can be exported in the following way.

  1. const Subscription = require('egg').Subscription;
  2. class Schedule extends Subscription {
  3. // This method should be implemented
  4. // subscribe can be async function or generator function
  5. async subscribe() {}
  6. }

We recommend plugin developers to implement based on this model, For example, Schedule.