defaulting to /. This "route" is the mount-point for the
middleware, when given a value other than / the middleware
is only effective when that segment is present in the request's
pathname.
For example if we were to mount a function at /admin, it would
be invoked on /admin, and /admin/settings, however it would
not be invoked for /, or /posts.
Examples:
var app = connect();
app.use(connect.favicon());
app.use(connect.logger());
app.use(connect.static(__dirname + '/public'));
If we wanted to prefix static files with /public, we could
"mount" the static()
middleware:
app.use('/public', connect.static(__dirname + '/public'));
This api is chainable, so the following is valid:
connect()
.use(connect.favicon())
.use(connect.logger())
.use(connect.static(__dirname + '/public'))
.listen(3000);
app.use = function(route, fn){
// default route to '/'
if ('string' != typeof route) {
fn = route;
route = '/';
}
// wrap sub-apps
if ('function' == typeof fn.handle) {
var server = fn;
fn.route = route;
fn = function(req, res, next){
server.handle(req, res, next);
};
}
// wrap vanilla http.Servers
if (fn instanceof http.Server) {
fn = fn.listeners('request')[0];
}
// strip trailing slash
if ('/' == route[route.length - 1]) {
route = route.slice(0, -1);
}
// add the middleware
debug('use %s %s', route || '/', fn.name || 'anonymous');
this.stack.push({ route: route, handle: fn });
return this;
};
Listen for connections.
This method takes the same arguments
as node's http.Server#listen()
.
HTTP and HTTPS:
If you run your application both as HTTP
and HTTPS you may wrap them individually,
since your Connect "server" is really just
a JavaScript Function
.
var connect = require('connect')
, http = require('http')
, https = require('https');
var app = connect();
http.createServer(app).listen(80);
https.createServer(options, app).listen(443);
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
app.handle()
Handle server requests, punting them down
the middleware stack.
Source