express.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 'use strict';
  9. /**
  10. * Module dependencies.
  11. */
  12. var EventEmitter = require('events').EventEmitter;
  13. var mixin = require('merge-descriptors');
  14. var proto = require('./application');
  15. var Route = require('./router/route');
  16. var Router = require('./router');
  17. var req = require('./request');
  18. var res = require('./response');
  19. /**
  20. * Expose `createApplication()`.
  21. */
  22. exports = module.exports = createApplication;
  23. /**
  24. * Create an express application.
  25. *
  26. * @return {Function}
  27. * @api public
  28. */
  29. function createApplication() {
  30. var app = function(req, res, next) {
  31. app.handle(req, res, next);
  32. };
  33. mixin(app, EventEmitter.prototype, false);
  34. mixin(app, proto, false);
  35. // expose the prototype that will get set on requests
  36. app.request = Object.create(req, {
  37. app: { configurable: true, enumerable: true, writable: true, value: app }
  38. })
  39. // expose the prototype that will get set on responses
  40. app.response = Object.create(res, {
  41. app: { configurable: true, enumerable: true, writable: true, value: app }
  42. })
  43. app.init();
  44. return app;
  45. }
  46. /**
  47. * Expose the prototypes.
  48. */
  49. exports.application = proto;
  50. exports.request = req;
  51. exports.response = res;
  52. /**
  53. * Expose constructors.
  54. */
  55. exports.Route = Route;
  56. exports.Router = Router;
  57. /**
  58. * Expose middleware
  59. */
  60. exports.query = require('./middleware/query');
  61. exports.static = require('serve-static');
  62. /**
  63. * Replace removed middleware with an appropriate error message.
  64. */
  65. [
  66. 'json',
  67. 'urlencoded',
  68. 'bodyParser',
  69. 'compress',
  70. 'cookieSession',
  71. 'session',
  72. 'logger',
  73. 'cookieParser',
  74. 'favicon',
  75. 'responseTime',
  76. 'errorHandler',
  77. 'timeout',
  78. 'methodOverride',
  79. 'vhost',
  80. 'csrf',
  81. 'directory',
  82. 'limit',
  83. 'multipart',
  84. 'staticCache',
  85. ].forEach(function (name) {
  86. Object.defineProperty(exports, name, {
  87. get: function () {
  88. throw new Error('Most middleware (like ' + name + ') is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.');
  89. },
  90. configurable: true
  91. });
  92. });