express.js 2.3 KB

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