File tree 3 files changed +52
-5
lines changed
3 files changed +52
-5
lines changed Original file line number Diff line number Diff line change @@ -112,9 +112,22 @@ app.listen(3000);
112
112
the following are supported:
113
113
114
114
- ` app.env ` defaulting to the __ NODE_ENV__ or "development"
115
+ - ` app.keys ` array of signed cookie keys
115
116
- ` app.proxy ` when true proxy header fields will be trusted
116
117
- ` app.subdomainOffset ` offset of ` .subdomains ` to ignore [ 2]
117
118
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
+
118
131
## app.listen(...)
119
132
120
133
A Koa application is not a 1-to-1 representation of an HTTP server.
Original file line number Diff line number Diff line change @@ -34,13 +34,23 @@ module.exports = class Application extends Emitter {
34
34
* @api public
35
35
*/
36
36
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 = { } ) {
38
48
super ( ) ;
39
-
40
- this . proxy = false ;
49
+ this . proxy = options . proxy || false ;
41
50
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 ;
44
54
this . context = Object . create ( context ) ;
45
55
this . request = Object . create ( request ) ;
46
56
this . response = Object . create ( response ) ;
Original file line number Diff line number Diff line change @@ -53,4 +53,28 @@ describe('app', () => {
53
53
process . env . NODE_ENV = NODE_ENV ;
54
54
assert . equal ( app . env , 'development' ) ;
55
55
} ) ;
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
+ } ) ;
56
80
} ) ;
You can’t perform that action at this time.
0 commit comments