|
| 1 | +--- |
| 2 | +title: Application |
| 3 | +--- |
| 4 | + |
| 5 | +## Use Cases |
| 6 | + |
| 7 | +`Application` is an global object which inherits from [Koa.Application]. It can be used for extending functionalities which are meant to be shared across the application. |
| 8 | + |
| 9 | +The Egg application would only instantiate the `Application` once in a process. |
| 10 | + |
| 11 | +:::warning |
| 12 | +For processes started by Egg, they all have an instance of `Application`. |
| 13 | +::: |
| 14 | + |
| 15 | +## How To Access |
| 16 | + |
| 17 | +`Application` probably is the most common seen object in developing Egg applications. |
| 18 | + |
| 19 | +```js |
| 20 | +// app/controller/home.js |
| 21 | +class HomeController extends Controller { |
| 22 | + async index() { |
| 23 | + // 从 `Controller/Service` 基类继承的属性: `this.app` |
| 24 | + console.log(this.app.config.name); |
| 25 | + // 从 ctx 对象上获取 |
| 26 | + console.log(this.ctx.app.config.name); |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +[Routers][Router] and [Middlewares][Middleware] and other files that loaded through the `Loader` can export a function and take the `app` as an argument. |
| 32 | + |
| 33 | +*Router* |
| 34 | + |
| 35 | +```js |
| 36 | +// app/router.js |
| 37 | +module.exports = app => { |
| 38 | + const { router, controller } = app; |
| 39 | + router.get('/', controller.home.index); |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +*Middleware* |
| 44 | + |
| 45 | +```js |
| 46 | +// app/middleware/response_time.js |
| 47 | +module.exports = (options, app) => { |
| 48 | + // 加载期传递 app 实例 |
| 49 | + console.log(app); |
| 50 | + |
| 51 | + return async function responseTime(ctx, next) {}; |
| 52 | +}; |
| 53 | +``` |
| 54 | + |
| 55 | +## Common API |
| 56 | + |
| 57 | +### app.config |
| 58 | + |
| 59 | +[Configurations][Configuration] for the application. |
| 60 | + |
| 61 | +### app.router |
| 62 | + |
| 63 | +The [`Router`][Router] instance. |
| 64 | + |
| 65 | +### app.controller |
| 66 | + |
| 67 | +All the [`Controller`][Controller] instances. |
| 68 | + |
| 69 | +### app.logger |
| 70 | + |
| 71 | +The global logger for logging business unrelated information. |
| 72 | + |
| 73 | +Go to [Logger] to see more. |
| 74 | + |
| 75 | +### app.middleware |
| 76 | + |
| 77 | +All [Middlewares][Middleware]. |
| 78 | + |
| 79 | +### app.server |
| 80 | + |
| 81 | +An instance of [HTTP Server](https://nodejs.org/api/http.html#http_class_http_server) or [HTTPS Server](https://nodejs.org/api/https.html#https_class_https_server). |
| 82 | + |
| 83 | +It can be accessed after the [lifecycle](./lifecycle.md) event `serverDidReady`. |
| 84 | + |
| 85 | +### app.curl() |
| 86 | + |
| 87 | +An instance of [HttpClient](./httpclient.md). |
| 88 | + |
| 89 | +### app.createAnonymousContext() |
| 90 | + |
| 91 | +You can create an anonymous request by calling this function if you need to access the [Context] instance. |
| 92 | + |
| 93 | +```js |
| 94 | +const ctx = app.createAnonymousContext(); |
| 95 | +await ctx.service.user.list(); |
| 96 | +``` |
| 97 | + |
| 98 | +## Extending |
| 99 | + |
| 100 | +Egg supports extending `Application` using `app/extend/application.js`. |
| 101 | + |
| 102 | +### Extend a Function |
| 103 | + |
| 104 | +```js |
| 105 | +// app/extend/application.js |
| 106 | +module.exports = { |
| 107 | + foo(param) { |
| 108 | + // `this` points to the `app` |
| 109 | + }, |
| 110 | +}; |
| 111 | +``` |
| 112 | + |
| 113 | +### Extend a Property |
| 114 | + |
| 115 | +It is recommended to use *Symbol* and *Getter* to save properties that need to be instantiate. |
| 116 | + |
| 117 | +```js |
| 118 | +// app/extend/application.js |
| 119 | +const NUNJUCKS = Symbol('Application#nunjucks'); |
| 120 | +const nunjuck = require('nunjuck'); |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + get nunjucks() { |
| 124 | + if (!this[NUNJUCKS]) { |
| 125 | + // `this` points to the `app` |
| 126 | + this[NUNJUCKS] = new nunjucks.Environment(this.config.nunjucks); |
| 127 | + } |
| 128 | + return this[NUNJUCKS]; |
| 129 | + }, |
| 130 | + |
| 131 | + foo: 'bar', |
| 132 | +}; |
| 133 | +``` |
| 134 | + |
| 135 | +### Writing Tests |
| 136 | + |
| 137 | +We recommend you to keep your plugins high quality using [unit tests](../workflow/development/unittest.md). |
| 138 | + |
| 139 | +```js |
| 140 | +// test/app/extend/application.js |
| 141 | +const { app, assert } = require('egg-mock'); |
| 142 | + |
| 143 | +describe('test/app/extend/application.js', () => { |
| 144 | + it('should export nunjucks', () => { |
| 145 | + assert(app.nunjucks); |
| 146 | + assert(app.nunjucks.renderString('{{ name }}', { name: 'TZ' }) === 'TZ'); |
| 147 | + }); |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +Find more on this topic in [Development - Unit Test](../workflow/development/unittest.md). |
| 152 | + |
| 153 | +### Extend In Different Environment |
| 154 | + |
| 155 | +You can choose when to activate certain plugins or extend files. |
| 156 | + |
| 157 | +For example, `app/extend/application.unittest.js` will only be activated on `unittest` environment. |
| 158 | + |
| 159 | +```js |
| 160 | +// app/extend/application.unittest.js |
| 161 | +module.exports = { |
| 162 | + mockXX(k, v) { |
| 163 | + }, |
| 164 | +}; |
| 165 | +``` |
| 166 | + |
| 167 | +Meanwhile, other objects like `Application`, `Context`, `Request`, `Response`, `Helper` can also be extended using this technique. |
| 168 | + |
| 169 | +[Koa]: http://koajs.com |
| 170 | +[Koa.Application]: http://koajs.com/#application |
| 171 | +[Middleware]: ./middleware.md |
| 172 | +[Context]: ./context.md |
| 173 | +[Controller]: ./controller.md |
| 174 | +[Service]: ./service.md |
| 175 | +[Router]: ./router.md |
| 176 | +[路由]: ./router.md |
| 177 | +[Configuration]: ./config.md |
| 178 | +[Logger]: ./logger.md |
0 commit comments