Skip to content

Commit 5afff89

Browse files
djakedead-horse
authored andcommitted
feat: accept options in the Application constructor (#1372)
1 parent 3b23865 commit 5afff89

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

docs/api/index.md

+13
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,22 @@ app.listen(3000);
112112
the following are supported:
113113

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

119+
You can pass the settings to the constructor:
120+
```js
121+
const Koa = require('koa');
122+
const app = new Koa({ proxy: true });
123+
```
124+
or dynamically:
125+
```js
126+
const Koa = require('koa');
127+
const app = new Koa();
128+
app.proxy = true;
129+
```
130+
118131
## app.listen(...)
119132

120133
A Koa application is not a 1-to-1 representation of an HTTP server.

lib/application.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@ module.exports = class Application extends Emitter {
3434
* @api public
3535
*/
3636

37-
constructor() {
37+
/**
38+
*
39+
* @param {object} [options] Application options
40+
* @param {string} [options.env='development'] Environment
41+
* @param {string[]} [options.keys] Signed cookie keys
42+
* @param {boolean} [options.proxy] Trust proxy headers
43+
* @param {number} [options.subdomainOffset] Subdomain offset
44+
*
45+
*/
46+
47+
constructor(options = {}) {
3848
super();
39-
40-
this.proxy = false;
49+
this.proxy = options.proxy || false;
4150
this.middleware = [];
42-
this.subdomainOffset = 2;
43-
this.env = process.env.NODE_ENV || 'development';
51+
this.subdomainOffset = options.subdomainOffset || 2;
52+
this.env = options.env || process.env.NODE_ENV || 'development';
53+
this.keys = options.keys || undefined;
4454
this.context = Object.create(context);
4555
this.request = Object.create(request);
4656
this.response = Object.create(response);

test/application/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ describe('app', () => {
5353
process.env.NODE_ENV = NODE_ENV;
5454
assert.equal(app.env, 'development');
5555
});
56+
57+
it('should set env from the constructor', () => {
58+
const env = 'custom';
59+
const app = new Koa({ env });
60+
assert.strictEqual(app.env, env);
61+
});
62+
63+
it('should set proxy flag from the constructor', () => {
64+
const proxy = true;
65+
const app = new Koa({ proxy });
66+
assert.strictEqual(app.proxy, proxy);
67+
});
68+
69+
it('should set signed cookie keys from the constructor', () => {
70+
const keys = ['customkey'];
71+
const app = new Koa({ keys });
72+
assert.strictEqual(app.keys, keys);
73+
});
74+
75+
it('should set subdomainOffset from the constructor', () => {
76+
const subdomainOffset = 3;
77+
const app = new Koa({ subdomainOffset });
78+
assert.strictEqual(app.subdomainOffset, subdomainOffset);
79+
});
5680
});

0 commit comments

Comments
 (0)