3 * Copyright(c) 2010 Sencha Inc.
4 * Copyright(c) 2011 TJ Holowaychuk
12 var finalhandler = require('finalhandler');
13 var http = require('http');
14 var debug = require('debug')('connect:dispatcher');
15 var parseUrl = require('parseurl');
19 var app = module.exports = {};
23 var env = process.env.NODE_ENV || 'development';
25 /* istanbul ignore next */
26 var defer = typeof setImmediate === 'function'
28 : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
31 * Utilize the given middleware `handle` to the given `route`,
32 * defaulting to _/_. This "route" is the mount-point for the
33 * middleware, when given a value other than _/_ the middleware
34 * is only effective when that segment is present in the request's
37 * For example if we were to mount a function at _/admin_, it would
38 * be invoked on _/admin_, and _/admin/settings_, however it would
39 * not be invoked for _/_, or _/posts_.
43 * var app = connect();
44 * app.use(connect.favicon());
45 * app.use(connect.logger());
46 * app.use(connect.static(__dirname + '/public'));
48 * If we wanted to prefix static files with _/public_, we could
49 * "mount" the `static()` middleware:
51 * app.use('/public', connect.static(__dirname + '/public'));
53 * This api is chainable, so the following is valid:
56 * .use(connect.favicon())
57 * .use(connect.logger())
58 * .use(connect.static(__dirname + '/public'))
61 * @param {String|Function|Server} route, callback or server
62 * @param {Function|Server} callback or server
63 * @return {Server} for chaining
67 app.use = function(route, fn){
68 // default route to '/'
69 if ('string' != typeof route) {
75 if ('function' == typeof fn.handle) {
78 fn = function(req, res, next){
79 server.handle(req, res, next);
83 // wrap vanilla http.Servers
84 if (fn instanceof http.Server) {
85 fn = fn.listeners('request')[0];
88 // strip trailing slash
89 if ('/' == route[route.length - 1]) {
90 route = route.slice(0, -1);
94 debug('use %s %s', route || '/', fn.name || 'anonymous');
95 this.stack.push({ route: route, handle: fn });
101 * Handle server requests, punting them down
102 * the middleware stack.
107 app.handle = function(req, res, out) {
108 var stack = this.stack
109 , searchIndex = req.url.indexOf('?')
110 , pathlength = searchIndex !== -1 ? searchIndex : req.url.length
111 , fqdn = req.url[0] !== '/' && 1 + req.url.substr(0, pathlength).indexOf('://')
112 , protohost = fqdn ? req.url.substr(0, req.url.indexOf('/', 2 + fqdn)) : ''
117 // final function handler
118 var done = out || finalhandler(req, res, {
127 req.url = req.url.substr(1);
131 req.url = protohost + removed + req.url.substr(protohost.length);
132 req.originalUrl = req.originalUrl || req.url;
136 layer = stack[index++];
145 path = parseUrl(req).pathname;
146 if (undefined == path) path = '/';
148 // skip this layer if the route doesn't match.
149 if (0 != path.toLowerCase().indexOf(layer.route.toLowerCase())) return next(err);
151 c = path[layer.route.length];
152 if (c && '/' != c && '.' != c) return next(err);
154 // Call the layer handler
155 // Trim off the part of the url that matches the route
156 removed = layer.route;
157 req.url = protohost + req.url.substr(protohost.length + removed.length);
159 // Ensure leading slash
160 if (!fqdn && '/' != req.url[0]) {
161 req.url = '/' + req.url;
165 debug('%s %s : %s', layer.handle.name || 'anonymous', layer.route, req.originalUrl);
166 var arity = layer.handle.length;
169 layer.handle(err, req, res, next);
173 } else if (arity < 4) {
174 layer.handle(req, res, next);
186 * Listen for connections.
188 * This method takes the same arguments
189 * as node's `http.Server#listen()`.
193 * If you run your application both as HTTP
194 * and HTTPS you may wrap them individually,
195 * since your Connect "server" is really just
196 * a JavaScript `Function`.
198 * var connect = require('connect')
199 * , http = require('http')
200 * , https = require('https');
202 * var app = connect();
204 * http.createServer(app).listen(80);
205 * https.createServer(options, app).listen(443);
207 * @return {http.Server}
211 app.listen = function(){
212 var server = http.createServer(this);
213 return server.listen.apply(server, arguments);
217 * Log error using console.error.
223 function logerror(err){
224 if (env !== 'test') console.error(err.stack || err.toString());