Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accept options in the Application constructor #1372

Merged
merged 5 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
*
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, add a newline between constructor-method and */.


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);
});
});