Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / lib / router / route.js
1 /**
2  * Module dependencies.
3  */
4
5 var debug = require('debug')('express:router:route');
6 var Layer = require('./layer');
7 var methods = require('methods');
8 var utils = require('../utils');
9
10 /**
11  * Expose `Route`.
12  */
13
14 module.exports = Route;
15
16 /**
17  * Initialize `Route` with the given `path`,
18  *
19  * @param {String} path
20  * @api private
21  */
22
23 function Route(path) {
24   debug('new %s', path);
25   this.path = path;
26   this.stack = [];
27
28   // route handlers for various http methods
29   this.methods = {};
30 }
31
32 /**
33  * @api private
34  */
35
36 Route.prototype._handles_method = function _handles_method(method) {
37   if (this.methods._all) {
38     return true;
39   }
40
41   method = method.toLowerCase();
42
43   if (method === 'head' && !this.methods['head']) {
44     method = 'get';
45   }
46
47   return Boolean(this.methods[method]);
48 };
49
50 /**
51  * @return {Array} supported HTTP methods
52  * @api private
53  */
54
55 Route.prototype._options = function _options() {
56   var methods = Object.keys(this.methods);
57
58   // append automatic head
59   if (this.methods.get && !this.methods.head) {
60     methods.push('head');
61   }
62
63   for (var i = 0; i < methods.length; i++) {
64     // make upper case
65     methods[i] = methods[i].toUpperCase();
66   }
67
68   return methods;
69 };
70
71 /**
72  * dispatch req, res into this route
73  *
74  * @api private
75  */
76
77 Route.prototype.dispatch = function(req, res, done){
78   var idx = 0;
79   var stack = this.stack;
80   if (stack.length === 0) {
81     return done();
82   }
83
84   var method = req.method.toLowerCase();
85   if (method === 'head' && !this.methods['head']) {
86     method = 'get';
87   }
88
89   req.route = this;
90
91   next();
92
93   function next(err) {
94     if (err && err === 'route') {
95       return done();
96     }
97
98     var layer = stack[idx++];
99     if (!layer) {
100       return done(err);
101     }
102
103     if (layer.method && layer.method !== method) {
104       return next(err);
105     }
106
107     if (err) {
108       layer.handle_error(err, req, res, next);
109     } else {
110       layer.handle_request(req, res, next);
111     }
112   }
113 };
114
115 /**
116  * Add a handler for all HTTP verbs to this route.
117  *
118  * Behaves just like middleware and can respond or call `next`
119  * to continue processing.
120  *
121  * You can use multiple `.all` call to add multiple handlers.
122  *
123  *   function check_something(req, res, next){
124  *     next();
125  *   };
126  *
127  *   function validate_user(req, res, next){
128  *     next();
129  *   };
130  *
131  *   route
132  *   .all(validate_user)
133  *   .all(check_something)
134  *   .get(function(req, res, next){
135  *     res.send('hello world');
136  *   });
137  *
138  * @param {function} handler
139  * @return {Route} for chaining
140  * @api public
141  */
142
143 Route.prototype.all = function(){
144   var callbacks = utils.flatten([].slice.call(arguments));
145   callbacks.forEach(function(fn) {
146     if (typeof fn !== 'function') {
147       var type = {}.toString.call(fn);
148       var msg = 'Route.all() requires callback functions but got a ' + type;
149       throw new Error(msg);
150     }
151
152     var layer = Layer('/', {}, fn);
153     layer.method = undefined;
154
155     this.methods._all = true;
156     this.stack.push(layer);
157   }, this);
158
159   return this;
160 };
161
162 methods.forEach(function(method){
163   Route.prototype[method] = function(){
164     var callbacks = utils.flatten([].slice.call(arguments));
165
166     callbacks.forEach(function(fn) {
167       if (typeof fn !== 'function') {
168         var type = {}.toString.call(fn);
169         var msg = 'Route.' + method + '() requires callback functions but got a ' + type;
170         throw new Error(msg);
171       }
172
173       debug('%s %s', method, this.path);
174
175       var layer = Layer('/', {}, fn);
176       layer.method = method;
177
178       this.methods[method] = true;
179       this.stack.push(layer);
180     }, this);
181     return this;
182   };
183 });