You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
console.log('Now listening on port '+app.get('port'));
44
+
});
45
+
```
46
+
47
+
This module aims to let you break each individual middleware configuration into their own file for tidier code. Given an example directory structure like this:
48
+
49
+
```shell
50
+
├── app.js
51
+
├── initializers
52
+
│ ├── favicon.js
53
+
│ ├── port.js
54
+
│ ├── routes.js
55
+
│ └── views.js
56
+
```
57
+
58
+
Your app setup file would look something like this:
59
+
60
+
```js
61
+
var express =require('express'),
62
+
initialize =require('express-initializers'),
63
+
64
+
app =express();
65
+
66
+
// Let the initializers run
67
+
initialize(app)
68
+
.then(function () {
69
+
// Start listening for requests
70
+
app.listen(app.get('port'), function () {
71
+
console.log('Now listening on port '+app.get('port'));
72
+
});
73
+
})
74
+
.catch(function (err) {
75
+
console.log('Unable to initialize app: '+err.message);
76
+
console.log(err.stack);
77
+
});
78
+
```
79
+
80
+
And each middleware configuration is moved into its own file. From simple examples like `port.js`:
81
+
82
+
```js
83
+
// initializers/port.js
84
+
module.exports= {
85
+
configure:function (app) {
86
+
app.set('port', process.env.PORT||3000);
87
+
}
88
+
};
89
+
```
90
+
91
+
To more complex things like view engines or db initialization:
returnreject(newError('Failed to initialize database: '+err.message));
135
+
}
136
+
137
+
// Sync all the associations
138
+
db.sync(function (err) {
139
+
if (err) {
140
+
returnreject(newError('Failed to sync database: '+err.message));
141
+
}
142
+
143
+
app.set('db', db.instance);
144
+
145
+
resolve();
146
+
});
147
+
});
148
+
});
149
+
}
150
+
};
151
+
```
152
+
153
+
A more thorough implementation can be seen at [node-site](https://github.com/jgable/node-site).
154
+
155
+
### Configuration
156
+
157
+
The `initializers` function returned from `require('express-initializers')` can accept options as the second parameter, and an optional callback as the third parameter (if you really hate promises).
158
+
159
+
```js
160
+
initialize(app, {
161
+
// Defaults to the 'initializers' directory relative to the calling file
162
+
directory:path.join(__dirname, 'configurers'),
163
+
// Defaults to '**/*.js'
164
+
fileMatch:'**/*.coffee'
165
+
}, function (err) {
166
+
if (err) {
167
+
throw err;
168
+
}
169
+
170
+
app.listen(app.get('port'));
171
+
});
172
+
```
173
+
174
+
### Initializers
175
+
176
+
Each individual initializer must be a module that exports an object of the form:
177
+
178
+
```js
179
+
module.exports= {
180
+
name:'something',
181
+
after:'otherthing',
182
+
183
+
configure:function (app) {
184
+
app.set('something', 42);
185
+
}
186
+
};
187
+
```
188
+
189
+
The `name` property can be unique or shared amongst a group of initializers.
190
+
191
+
The `after` property allows you to order your initializers, it signals that this initializer should be ran after another or a group of other initializers.
192
+
193
+
The `configure` method can optionally return a promise for asynchronous configuration.
194
+
195
+
### LICENSE
196
+
197
+
MIT License, Copyright 2014 [Jacob Gable](https://jacobgable.com)
0 commit comments