xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / connect / lib / connect.js

2.2 KB
1
2/*!
3 * Connect
4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
6 * MIT Licensed
7 */
8
9/**
10 * Module dependencies.
11 */
12
13var HTTPServer = require('./http').Server
14  , HTTPSServer = require('./https').Server
15  , fs = require('fs');
16
17// node patches
18
19require('./patch');
20
21// expose createServer() as the module
22
23exports = module.exports = createServer;
24
25/**
26 * Framework version.
27 */
28
29exports.version = '1.4.2';
30
31/**
32 * Initialize a new `connect.HTTPServer` with the middleware
33 * passed to this function. When an object is passed _first_,
34 * we assume these are the tls options, and return a `connect.HTTPSServer`.
35 *
36 * Examples:
37 *
38 * An example HTTP server, accepting several middleware.
39 *
40 *     var server = connect.createServer(
41 *         connect.logger()
42 *       , connect.static(__dirname + '/public')
43 *     );
44 *
45 * An HTTPS server, utilizing the same middleware as above.
46 *
47 *     var server = connect.createServer(
48 *         { key: key, cert: cert }
49 *       , connect.logger()
50 *       , connect.static(__dirname + '/public')
51 *     );
52 *
53 * Alternatively with connect 1.0 we may omit `createServer()`.
54 *
55 *     connect(
56 *         connect.logger()
57 *       , connect.static(__dirname + '/public')
58 *     ).listen(3000);
59 *
60 * @param  {Object|Function} ...
61 * @return {Server}
62 * @api public
63 */
64
65function createServer() {
66  if ('object' == typeof arguments[0]) {
67    return new HTTPSServer(arguments[0], Array.prototype.slice.call(arguments, 1));
68  } else {
69    return new HTTPServer(Array.prototype.slice.call(arguments));
70  }
71};
72
73// support connect.createServer()
74
75exports.createServer = createServer;
76
77// auto-load getters
78
79exports.middleware = {};
80
81/**
82 * Auto-load bundled middleware with getters.
83 */
84
85fs.readdirSync(__dirname + '/middleware').forEach(function(filename){
86  if (/\.js$/.test(filename)) {
87    var name = filename.substr(0, filename.lastIndexOf('.'));
88    exports.middleware.__defineGetter__(name, function(){
89      return require('./middleware/' + name);
90    });
91  }
92});
93
94// expose utils
95
96exports.utils = require('./utils');
97
98// expose getters as first-class exports
99
100exports.utils.merge(exports, exports.middleware);
101
102// expose constructors
103
104exports.HTTPServer = HTTPServer;
105exports.HTTPSServer = HTTPSServer;
106