comparison node_modules/express/lib/router/route.js @ 73:0c3a2942ddee

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