Skip to content

Commit

Permalink
feat: accept options in the Application constructor (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
djake authored and dead-horse committed Aug 19, 2019
1 parent 3b23865 commit 5afff89
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
13 changes: 13 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ app.listen(3000);
the following are supported:

- `app.env` defaulting to the __NODE_ENV__ or "development"
- `app.keys` array of signed cookie keys
- `app.proxy` when true proxy header fields will be trusted
- `app.subdomainOffset` offset of `.subdomains` to ignore [2]

You can pass the settings to the constructor:
```js
const Koa = require('koa');
const app = new Koa({ proxy: true });
```
or dynamically:
```js
const Koa = require('koa');
const app = new Koa();
app.proxy = true;
```

## app.listen(...)

A Koa application is not a 1-to-1 representation of an HTTP server.
Expand Down
20 changes: 15 additions & 5 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ module.exports = class Application extends Emitter {
* @api public
*/

constructor() {
/**
*
* @param {object} [options] Application options
* @param {string} [options.env='development'] Environment
* @param {string[]} [options.keys] Signed cookie keys
* @param {boolean} [options.proxy] Trust proxy headers
* @param {number} [options.subdomainOffset] Subdomain offset
*
*/

constructor(options = {}) {
super();

this.proxy = false;
this.proxy = options.proxy || false;
this.middleware = [];
this.subdomainOffset = 2;
this.env = process.env.NODE_ENV || 'development';
this.subdomainOffset = options.subdomainOffset || 2;
this.env = options.env || process.env.NODE_ENV || 'development';
this.keys = options.keys || undefined;
this.context = Object.create(context);
this.request = Object.create(request);
this.response = Object.create(response);
Expand Down
24 changes: 24 additions & 0 deletions test/application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,28 @@ describe('app', () => {
process.env.NODE_ENV = NODE_ENV;
assert.equal(app.env, 'development');
});

it('should set env from the constructor', () => {
const env = 'custom';
const app = new Koa({ env });
assert.strictEqual(app.env, env);
});

it('should set proxy flag from the constructor', () => {
const proxy = true;
const app = new Koa({ proxy });
assert.strictEqual(app.proxy, proxy);
});

it('should set signed cookie keys from the constructor', () => {
const keys = ['customkey'];
const app = new Koa({ keys });
assert.strictEqual(app.keys, keys);
});

it('should set subdomainOffset from the constructor', () => {
const subdomainOffset = 3;
const app = new Koa({ subdomainOffset });
assert.strictEqual(app.subdomainOffset, subdomainOffset);
});
});

0 comments on commit 5afff89

Please sign in to comment.