express.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('node:events').EventEmitter;
  14. var mixin = require('merge-descriptors');
  15. var proto = require('./application');
  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 = Router.Route;
  56. exports.Router = Router;
  57. /**
  58. * Expose middleware
  59. */
  60. exports.json = bodyParser.json
  61. exports.raw = bodyParser.raw
  62. exports.static = require('serve-static');
  63. exports.text = bodyParser.text
  64. exports.urlencoded = bodyParser.urlencoded