express.exe 0.0.1769201820754

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,631 @@
1
+ /*!
2
+ * express
3
+ * Copyright(c) 2009-2013 TJ Holowaychuk
4
+ * Copyright(c) 2013 Roman Shtylman
5
+ * Copyright(c) 2014-2015 Douglas Christopher Wilson
6
+ * MIT Licensed
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ /**
12
+ * Module dependencies.
13
+ * @private
14
+ */
15
+
16
+ var finalhandler = require('finalhandler');
17
+ var debug = require('debug')('express:application');
18
+ var View = require('./view');
19
+ var http = require('node:http');
20
+ var methods = require('./utils').methods;
21
+ var compileETag = require('./utils').compileETag;
22
+ var compileQueryParser = require('./utils').compileQueryParser;
23
+ var compileTrust = require('./utils').compileTrust;
24
+ var resolve = require('node:path').resolve;
25
+ var once = require('once')
26
+ var Router = require('router');
27
+
28
+ /**
29
+ * Module variables.
30
+ * @private
31
+ */
32
+
33
+ var slice = Array.prototype.slice;
34
+ var flatten = Array.prototype.flat;
35
+
36
+ /**
37
+ * Application prototype.
38
+ */
39
+
40
+ var app = exports = module.exports = {};
41
+
42
+ /**
43
+ * Variable for trust proxy inheritance back-compat
44
+ * @private
45
+ */
46
+
47
+ var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';
48
+
49
+ /**
50
+ * Initialize the server.
51
+ *
52
+ * - setup default configuration
53
+ * - setup default middleware
54
+ * - setup route reflection methods
55
+ *
56
+ * @private
57
+ */
58
+
59
+ app.init = function init() {
60
+ var router = null;
61
+
62
+ this.cache = Object.create(null);
63
+ this.engines = Object.create(null);
64
+ this.settings = Object.create(null);
65
+
66
+ this.defaultConfiguration();
67
+
68
+ // Setup getting to lazily add base router
69
+ Object.defineProperty(this, 'router', {
70
+ configurable: true,
71
+ enumerable: true,
72
+ get: function getrouter() {
73
+ if (router === null) {
74
+ router = new Router({
75
+ caseSensitive: this.enabled('case sensitive routing'),
76
+ strict: this.enabled('strict routing')
77
+ });
78
+ }
79
+
80
+ return router;
81
+ }
82
+ });
83
+ };
84
+
85
+ /**
86
+ * Initialize application configuration.
87
+ * @private
88
+ */
89
+
90
+ app.defaultConfiguration = function defaultConfiguration() {
91
+ var env = process.env.NODE_ENV || 'development';
92
+
93
+ // default settings
94
+ this.enable('x-powered-by');
95
+ this.set('etag', 'weak');
96
+ this.set('env', env);
97
+ this.set('query parser', 'simple')
98
+ this.set('subdomain offset', 2);
99
+ this.set('trust proxy', false);
100
+
101
+ // trust proxy inherit back-compat
102
+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {
103
+ configurable: true,
104
+ value: true
105
+ });
106
+
107
+ debug('booting in %s mode', env);
108
+
109
+ this.on('mount', function onmount(parent) {
110
+ // inherit trust proxy
111
+ if (this.settings[trustProxyDefaultSymbol] === true
112
+ && typeof parent.settings['trust proxy fn'] === 'function') {
113
+ delete this.settings['trust proxy'];
114
+ delete this.settings['trust proxy fn'];
115
+ }
116
+
117
+ // inherit protos
118
+ Object.setPrototypeOf(this.request, parent.request)
119
+ Object.setPrototypeOf(this.response, parent.response)
120
+ Object.setPrototypeOf(this.engines, parent.engines)
121
+ Object.setPrototypeOf(this.settings, parent.settings)
122
+ });
123
+
124
+ // setup locals
125
+ this.locals = Object.create(null);
126
+
127
+ // top-most app is mounted at /
128
+ this.mountpath = '/';
129
+
130
+ // default locals
131
+ this.locals.settings = this.settings;
132
+
133
+ // default configuration
134
+ this.set('view', View);
135
+ this.set('views', resolve('views'));
136
+ this.set('jsonp callback name', 'callback');
137
+
138
+ if (env === 'production') {
139
+ this.enable('view cache');
140
+ }
141
+ };
142
+
143
+ /**
144
+ * Dispatch a req, res pair into the application. Starts pipeline processing.
145
+ *
146
+ * If no callback is provided, then default error handlers will respond
147
+ * in the event of an error bubbling through the stack.
148
+ *
149
+ * @private
150
+ */
151
+
152
+ app.handle = function handle(req, res, callback) {
153
+ // final handler
154
+ var done = callback || finalhandler(req, res, {
155
+ env: this.get('env'),
156
+ onerror: logerror.bind(this)
157
+ });
158
+
159
+ // set powered by header
160
+ if (this.enabled('x-powered-by')) {
161
+ res.setHeader('X-Powered-By', 'Express');
162
+ }
163
+
164
+ // set circular references
165
+ req.res = res;
166
+ res.req = req;
167
+
168
+ // alter the prototypes
169
+ Object.setPrototypeOf(req, this.request)
170
+ Object.setPrototypeOf(res, this.response)
171
+
172
+ // setup locals
173
+ if (!res.locals) {
174
+ res.locals = Object.create(null);
175
+ }
176
+
177
+ this.router.handle(req, res, done);
178
+ };
179
+
180
+ /**
181
+ * Proxy `Router#use()` to add middleware to the app router.
182
+ * See Router#use() documentation for details.
183
+ *
184
+ * If the _fn_ parameter is an express app, then it will be
185
+ * mounted at the _route_ specified.
186
+ *
187
+ * @public
188
+ */
189
+
190
+ app.use = function use(fn) {
191
+ var offset = 0;
192
+ var path = '/';
193
+
194
+ // default path to '/'
195
+ // disambiguate app.use([fn])
196
+ if (typeof fn !== 'function') {
197
+ var arg = fn;
198
+
199
+ while (Array.isArray(arg) && arg.length !== 0) {
200
+ arg = arg[0];
201
+ }
202
+
203
+ // first arg is the path
204
+ if (typeof arg !== 'function') {
205
+ offset = 1;
206
+ path = fn;
207
+ }
208
+ }
209
+
210
+ var fns = flatten.call(slice.call(arguments, offset), Infinity);
211
+
212
+ if (fns.length === 0) {
213
+ throw new TypeError('app.use() requires a middleware function')
214
+ }
215
+
216
+ // get router
217
+ var router = this.router;
218
+
219
+ fns.forEach(function (fn) {
220
+ // non-express app
221
+ if (!fn || !fn.handle || !fn.set) {
222
+ return router.use(path, fn);
223
+ }
224
+
225
+ debug('.use app under %s', path);
226
+ fn.mountpath = path;
227
+ fn.parent = this;
228
+
229
+ // restore .app property on req and res
230
+ router.use(path, function mounted_app(req, res, next) {
231
+ var orig = req.app;
232
+ fn.handle(req, res, function (err) {
233
+ Object.setPrototypeOf(req, orig.request)
234
+ Object.setPrototypeOf(res, orig.response)
235
+ next(err);
236
+ });
237
+ });
238
+
239
+ // mounted an app
240
+ fn.emit('mount', this);
241
+ }, this);
242
+
243
+ return this;
244
+ };
245
+
246
+ /**
247
+ * Proxy to the app `Router#route()`
248
+ * Returns a new `Route` instance for the _path_.
249
+ *
250
+ * Routes are isolated middleware stacks for specific paths.
251
+ * See the Route api docs for details.
252
+ *
253
+ * @public
254
+ */
255
+
256
+ app.route = function route(path) {
257
+ return this.router.route(path);
258
+ };
259
+
260
+ /**
261
+ * Register the given template engine callback `fn`
262
+ * as `ext`.
263
+ *
264
+ * By default will `require()` the engine based on the
265
+ * file extension. For example if you try to render
266
+ * a "foo.ejs" file Express will invoke the following internally:
267
+ *
268
+ * app.engine('ejs', require('ejs').__express);
269
+ *
270
+ * For engines that do not provide `.__express` out of the box,
271
+ * or if you wish to "map" a different extension to the template engine
272
+ * you may use this method. For example mapping the EJS template engine to
273
+ * ".html" files:
274
+ *
275
+ * app.engine('html', require('ejs').renderFile);
276
+ *
277
+ * In this case EJS provides a `.renderFile()` method with
278
+ * the same signature that Express expects: `(path, options, callback)`,
279
+ * though note that it aliases this method as `ejs.__express` internally
280
+ * so if you're using ".ejs" extensions you don't need to do anything.
281
+ *
282
+ * Some template engines do not follow this convention, the
283
+ * [Consolidate.js](https://github.com/tj/consolidate.js)
284
+ * library was created to map all of node's popular template
285
+ * engines to follow this convention, thus allowing them to
286
+ * work seamlessly within Express.
287
+ *
288
+ * @param {String} ext
289
+ * @param {Function} fn
290
+ * @return {app} for chaining
291
+ * @public
292
+ */
293
+
294
+ app.engine = function engine(ext, fn) {
295
+ if (typeof fn !== 'function') {
296
+ throw new Error('callback function required');
297
+ }
298
+
299
+ // get file extension
300
+ var extension = ext[0] !== '.'
301
+ ? '.' + ext
302
+ : ext;
303
+
304
+ // store engine
305
+ this.engines[extension] = fn;
306
+
307
+ return this;
308
+ };
309
+
310
+ /**
311
+ * Proxy to `Router#param()` with one added api feature. The _name_ parameter
312
+ * can be an array of names.
313
+ *
314
+ * See the Router#param() docs for more details.
315
+ *
316
+ * @param {String|Array} name
317
+ * @param {Function} fn
318
+ * @return {app} for chaining
319
+ * @public
320
+ */
321
+
322
+ app.param = function param(name, fn) {
323
+ if (Array.isArray(name)) {
324
+ for (var i = 0; i < name.length; i++) {
325
+ this.param(name[i], fn);
326
+ }
327
+
328
+ return this;
329
+ }
330
+
331
+ this.router.param(name, fn);
332
+
333
+ return this;
334
+ };
335
+
336
+ /**
337
+ * Assign `setting` to `val`, or return `setting`'s value.
338
+ *
339
+ * app.set('foo', 'bar');
340
+ * app.set('foo');
341
+ * // => "bar"
342
+ *
343
+ * Mounted servers inherit their parent server's settings.
344
+ *
345
+ * @param {String} setting
346
+ * @param {*} [val]
347
+ * @return {Server} for chaining
348
+ * @public
349
+ */
350
+
351
+ app.set = function set(setting, val) {
352
+ if (arguments.length === 1) {
353
+ // app.get(setting)
354
+ return this.settings[setting];
355
+ }
356
+
357
+ debug('set "%s" to %o', setting, val);
358
+
359
+ // set value
360
+ this.settings[setting] = val;
361
+
362
+ // trigger matched settings
363
+ switch (setting) {
364
+ case 'etag':
365
+ this.set('etag fn', compileETag(val));
366
+ break;
367
+ case 'query parser':
368
+ this.set('query parser fn', compileQueryParser(val));
369
+ break;
370
+ case 'trust proxy':
371
+ this.set('trust proxy fn', compileTrust(val));
372
+
373
+ // trust proxy inherit back-compat
374
+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {
375
+ configurable: true,
376
+ value: false
377
+ });
378
+
379
+ break;
380
+ }
381
+
382
+ return this;
383
+ };
384
+
385
+ /**
386
+ * Return the app's absolute pathname
387
+ * based on the parent(s) that have
388
+ * mounted it.
389
+ *
390
+ * For example if the application was
391
+ * mounted as "/admin", which itself
392
+ * was mounted as "/blog" then the
393
+ * return value would be "/blog/admin".
394
+ *
395
+ * @return {String}
396
+ * @private
397
+ */
398
+
399
+ app.path = function path() {
400
+ return this.parent
401
+ ? this.parent.path() + this.mountpath
402
+ : '';
403
+ };
404
+
405
+ /**
406
+ * Check if `setting` is enabled (truthy).
407
+ *
408
+ * app.enabled('foo')
409
+ * // => false
410
+ *
411
+ * app.enable('foo')
412
+ * app.enabled('foo')
413
+ * // => true
414
+ *
415
+ * @param {String} setting
416
+ * @return {Boolean}
417
+ * @public
418
+ */
419
+
420
+ app.enabled = function enabled(setting) {
421
+ return Boolean(this.set(setting));
422
+ };
423
+
424
+ /**
425
+ * Check if `setting` is disabled.
426
+ *
427
+ * app.disabled('foo')
428
+ * // => true
429
+ *
430
+ * app.enable('foo')
431
+ * app.disabled('foo')
432
+ * // => false
433
+ *
434
+ * @param {String} setting
435
+ * @return {Boolean}
436
+ * @public
437
+ */
438
+
439
+ app.disabled = function disabled(setting) {
440
+ return !this.set(setting);
441
+ };
442
+
443
+ /**
444
+ * Enable `setting`.
445
+ *
446
+ * @param {String} setting
447
+ * @return {app} for chaining
448
+ * @public
449
+ */
450
+
451
+ app.enable = function enable(setting) {
452
+ return this.set(setting, true);
453
+ };
454
+
455
+ /**
456
+ * Disable `setting`.
457
+ *
458
+ * @param {String} setting
459
+ * @return {app} for chaining
460
+ * @public
461
+ */
462
+
463
+ app.disable = function disable(setting) {
464
+ return this.set(setting, false);
465
+ };
466
+
467
+ /**
468
+ * Delegate `.VERB(...)` calls to `router.VERB(...)`.
469
+ */
470
+
471
+ methods.forEach(function (method) {
472
+ app[method] = function (path) {
473
+ if (method === 'get' && arguments.length === 1) {
474
+ // app.get(setting)
475
+ return this.set(path);
476
+ }
477
+
478
+ var route = this.route(path);
479
+ route[method].apply(route, slice.call(arguments, 1));
480
+ return this;
481
+ };
482
+ });
483
+
484
+ /**
485
+ * Special-cased "all" method, applying the given route `path`,
486
+ * middleware, and callback to _every_ HTTP method.
487
+ *
488
+ * @param {String} path
489
+ * @param {Function} ...
490
+ * @return {app} for chaining
491
+ * @public
492
+ */
493
+
494
+ app.all = function all(path) {
495
+ var route = this.route(path);
496
+ var args = slice.call(arguments, 1);
497
+
498
+ for (var i = 0; i < methods.length; i++) {
499
+ route[methods[i]].apply(route, args);
500
+ }
501
+
502
+ return this;
503
+ };
504
+
505
+ /**
506
+ * Render the given view `name` name with `options`
507
+ * and a callback accepting an error and the
508
+ * rendered template string.
509
+ *
510
+ * Example:
511
+ *
512
+ * app.render('email', { name: 'Tobi' }, function(err, html){
513
+ * // ...
514
+ * })
515
+ *
516
+ * @param {String} name
517
+ * @param {Object|Function} options or fn
518
+ * @param {Function} callback
519
+ * @public
520
+ */
521
+
522
+ app.render = function render(name, options, callback) {
523
+ var cache = this.cache;
524
+ var done = callback;
525
+ var engines = this.engines;
526
+ var opts = options;
527
+ var view;
528
+
529
+ // support callback function as second arg
530
+ if (typeof options === 'function') {
531
+ done = options;
532
+ opts = {};
533
+ }
534
+
535
+ // merge options
536
+ var renderOptions = { ...this.locals, ...opts._locals, ...opts };
537
+
538
+ // set .cache unless explicitly provided
539
+ if (renderOptions.cache == null) {
540
+ renderOptions.cache = this.enabled('view cache');
541
+ }
542
+
543
+ // primed cache
544
+ if (renderOptions.cache) {
545
+ view = cache[name];
546
+ }
547
+
548
+ // view
549
+ if (!view) {
550
+ var View = this.get('view');
551
+
552
+ view = new View(name, {
553
+ defaultEngine: this.get('view engine'),
554
+ root: this.get('views'),
555
+ engines: engines
556
+ });
557
+
558
+ if (!view.path) {
559
+ var dirs = Array.isArray(view.root) && view.root.length > 1
560
+ ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'
561
+ : 'directory "' + view.root + '"'
562
+ var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);
563
+ err.view = view;
564
+ return done(err);
565
+ }
566
+
567
+ // prime the cache
568
+ if (renderOptions.cache) {
569
+ cache[name] = view;
570
+ }
571
+ }
572
+
573
+ // render
574
+ tryRender(view, renderOptions, done);
575
+ };
576
+
577
+ /**
578
+ * Listen for connections.
579
+ *
580
+ * A node `http.Server` is returned, with this
581
+ * application (which is a `Function`) as its
582
+ * callback. If you wish to create both an HTTP
583
+ * and HTTPS server you may do so with the "http"
584
+ * and "https" modules as shown here:
585
+ *
586
+ * var http = require('node:http')
587
+ * , https = require('node:https')
588
+ * , express = require('express')
589
+ * , app = express();
590
+ *
591
+ * http.createServer(app).listen(80);
592
+ * https.createServer({ ... }, app).listen(443);
593
+ *
594
+ * @return {http.Server}
595
+ * @public
596
+ */
597
+
598
+ app.listen = function listen() {
599
+ var server = http.createServer(this)
600
+ var args = slice.call(arguments)
601
+ if (typeof args[args.length - 1] === 'function') {
602
+ var done = args[args.length - 1] = once(args[args.length - 1])
603
+ server.once('error', done)
604
+ }
605
+ return server.listen.apply(server, args)
606
+ }
607
+
608
+ /**
609
+ * Log error using console.error.
610
+ *
611
+ * @param {Error} err
612
+ * @private
613
+ */
614
+
615
+ function logerror(err) {
616
+ /* istanbul ignore next */
617
+ if (this.get('env') !== 'test') console.error(err.stack || err.toString());
618
+ }
619
+
620
+ /**
621
+ * Try rendering a view.
622
+ * @private
623
+ */
624
+
625
+ function tryRender(view, options, callback) {
626
+ try {
627
+ view.render(options, callback);
628
+ } catch (err) {
629
+ callback(err);
630
+ }
631
+ }
package/lib/express.js ADDED
@@ -0,0 +1,81 @@
1
+ /*!
2
+ * express
3
+ * Copyright(c) 2009-2013 TJ Holowaychuk
4
+ * Copyright(c) 2013 Roman Shtylman
5
+ * Copyright(c) 2014-2015 Douglas Christopher Wilson
6
+ * MIT Licensed
7
+ */
8
+
9
+ 'use strict';
10
+
11
+ /**
12
+ * Module dependencies.
13
+ */
14
+
15
+ var bodyParser = require('body-parser')
16
+ var EventEmitter = require('node:events').EventEmitter;
17
+ var mixin = require('merge-descriptors');
18
+ var proto = require('./application');
19
+ var Router = require('router');
20
+ var req = require('./request');
21
+ var res = require('./response');
22
+
23
+ /**
24
+ * Expose `createApplication()`.
25
+ */
26
+
27
+ exports = module.exports = createApplication;
28
+
29
+ /**
30
+ * Create an express application.
31
+ *
32
+ * @return {Function}
33
+ * @api public
34
+ */
35
+
36
+ function createApplication() {
37
+ var app = function(req, res, next) {
38
+ app.handle(req, res, next);
39
+ };
40
+
41
+ mixin(app, EventEmitter.prototype, false);
42
+ mixin(app, proto, false);
43
+
44
+ // expose the prototype that will get set on requests
45
+ app.request = Object.create(req, {
46
+ app: { configurable: true, enumerable: true, writable: true, value: app }
47
+ })
48
+
49
+ // expose the prototype that will get set on responses
50
+ app.response = Object.create(res, {
51
+ app: { configurable: true, enumerable: true, writable: true, value: app }
52
+ })
53
+
54
+ app.init();
55
+ return app;
56
+ }
57
+
58
+ /**
59
+ * Expose the prototypes.
60
+ */
61
+
62
+ exports.application = proto;
63
+ exports.request = req;
64
+ exports.response = res;
65
+
66
+ /**
67
+ * Expose constructors.
68
+ */
69
+
70
+ exports.Route = Router.Route;
71
+ exports.Router = Router;
72
+
73
+ /**
74
+ * Expose middleware
75
+ */
76
+
77
+ exports.json = bodyParser.json
78
+ exports.raw = bodyParser.raw
79
+ exports.static = require('serve-static');
80
+ exports.text = bodyParser.text
81
+ exports.urlencoded = bodyParser.urlencoded