source: frontend/node_modules/express/lib/router/route.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
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
9'use strict';
10
11/**
12 * Module dependencies.
13 * @private
14 */
15
16var debug = require('debug')('express:router:route');
17var flatten = require('array-flatten');
18var Layer = require('./layer');
19var methods = require('methods');
20
21/**
22 * Module variables.
23 * @private
24 */
25
26var slice = Array.prototype.slice;
27var toString = Object.prototype.toString;
28
29/**
30 * Module exports.
31 * @public
32 */
33
34module.exports = Route;
35
36/**
37 * Initialize `Route` with the given `path`,
38 *
39 * @param {String} path
40 * @public
41 */
42
43function Route(path) {
44 this.path = path;
45 this.stack = [];
46
47 debug('new %o', path)
48
49 // route handlers for various http methods
50 this.methods = {};
51}
52
53/**
54 * Determine if the route handles a given method.
55 * @private
56 */
57
58Route.prototype._handles_method = function _handles_method(method) {
59 if (this.methods._all) {
60 return true;
61 }
62
63 // normalize name
64 var name = typeof method === 'string'
65 ? method.toLowerCase()
66 : method
67
68 if (name === 'head' && !this.methods['head']) {
69 name = 'get';
70 }
71
72 return Boolean(this.methods[name]);
73};
74
75/**
76 * @return {Array} supported HTTP methods
77 * @private
78 */
79
80Route.prototype._options = function _options() {
81 var methods = Object.keys(this.methods);
82
83 // append automatic head
84 if (this.methods.get && !this.methods.head) {
85 methods.push('head');
86 }
87
88 for (var i = 0; i < methods.length; i++) {
89 // make upper case
90 methods[i] = methods[i].toUpperCase();
91 }
92
93 return methods;
94};
95
96/**
97 * dispatch req, res into this route
98 * @private
99 */
100
101Route.prototype.dispatch = function dispatch(req, res, done) {
102 var idx = 0;
103 var stack = this.stack;
104 var sync = 0
105
106 if (stack.length === 0) {
107 return done();
108 }
109 var method = typeof req.method === 'string'
110 ? req.method.toLowerCase()
111 : req.method
112
113 if (method === 'head' && !this.methods['head']) {
114 method = 'get';
115 }
116
117 req.route = this;
118
119 next();
120
121 function next(err) {
122 // signal to exit route
123 if (err && err === 'route') {
124 return done();
125 }
126
127 // signal to exit router
128 if (err && err === 'router') {
129 return done(err)
130 }
131
132 // max sync stack
133 if (++sync > 100) {
134 return setImmediate(next, err)
135 }
136
137 var layer = stack[idx++]
138
139 // end of layers
140 if (!layer) {
141 return done(err)
142 }
143
144 if (layer.method && layer.method !== method) {
145 next(err)
146 } else if (err) {
147 layer.handle_error(err, req, res, next);
148 } else {
149 layer.handle_request(req, res, next);
150 }
151
152 sync = 0
153 }
154};
155
156/**
157 * Add a handler for all HTTP verbs to this route.
158 *
159 * Behaves just like middleware and can respond or call `next`
160 * to continue processing.
161 *
162 * You can use multiple `.all` call to add multiple handlers.
163 *
164 * function check_something(req, res, next){
165 * next();
166 * };
167 *
168 * function validate_user(req, res, next){
169 * next();
170 * };
171 *
172 * route
173 * .all(validate_user)
174 * .all(check_something)
175 * .get(function(req, res, next){
176 * res.send('hello world');
177 * });
178 *
179 * @param {function} handler
180 * @return {Route} for chaining
181 * @api public
182 */
183
184Route.prototype.all = function all() {
185 var handles = flatten(slice.call(arguments));
186
187 for (var i = 0; i < handles.length; i++) {
188 var handle = handles[i];
189
190 if (typeof handle !== 'function') {
191 var type = toString.call(handle);
192 var msg = 'Route.all() requires a callback function but got a ' + type
193 throw new TypeError(msg);
194 }
195
196 var layer = Layer('/', {}, handle);
197 layer.method = undefined;
198
199 this.methods._all = true;
200 this.stack.push(layer);
201 }
202
203 return this;
204};
205
206methods.forEach(function(method){
207 Route.prototype[method] = function(){
208 var handles = flatten(slice.call(arguments));
209
210 for (var i = 0; i < handles.length; i++) {
211 var handle = handles[i];
212
213 if (typeof handle !== 'function') {
214 var type = toString.call(handle);
215 var msg = 'Route.' + method + '() requires a callback function but got a ' + type
216 throw new Error(msg);
217 }
218
219 debug('%s %o', method, this.path)
220
221 var layer = Layer('/', {}, handle);
222 layer.method = method;
223
224 this.methods[method] = true;
225 this.stack.push(layer);
226 }
227
228 return this;
229 };
230});
Note: See TracBrowser for help on using the repository browser.