You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
1.6 KiB

  1. /*!
  2. * Connect
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var EventEmitter = require('events').EventEmitter
  11. , proto = require('./proto')
  12. , utils = require('./utils')
  13. , path = require('path')
  14. , basename = path.basename
  15. , fs = require('fs');
  16. // node patches
  17. require('./patch');
  18. // expose createServer() as the module
  19. exports = module.exports = createServer;
  20. /**
  21. * Framework version.
  22. */
  23. exports.version = '2.7.11';
  24. /**
  25. * Expose mime module.
  26. */
  27. exports.mime = require('./middleware/static').mime;
  28. /**
  29. * Expose the prototype.
  30. */
  31. exports.proto = proto;
  32. /**
  33. * Auto-load middleware getters.
  34. */
  35. exports.middleware = {};
  36. /**
  37. * Expose utilities.
  38. */
  39. exports.utils = utils;
  40. /**
  41. * Create a new connect server.
  42. *
  43. * @return {Function}
  44. * @api public
  45. */
  46. function createServer() {
  47. function app(req, res, next){ app.handle(req, res, next); }
  48. utils.merge(app, proto);
  49. utils.merge(app, EventEmitter.prototype);
  50. app.route = '/';
  51. app.stack = [];
  52. for (var i = 0; i < arguments.length; ++i) {
  53. app.use(arguments[i]);
  54. }
  55. return app;
  56. };
  57. /**
  58. * Support old `.createServer()` method.
  59. */
  60. createServer.createServer = createServer;
  61. /**
  62. * Auto-load bundled middleware with getters.
  63. */
  64. fs.readdirSync(__dirname + '/middleware').forEach(function(filename){
  65. if (!/\.js$/.test(filename)) return;
  66. var name = basename(filename, '.js');
  67. function load(){ return require('./middleware/' + name); }
  68. exports.middleware.__defineGetter__(name, load);
  69. exports.__defineGetter__(name, load);
  70. });