senchalabs/connect — Issue #469: static middleware not working properly
Author: davidcl64
Created: 2012-01-31 01:01:33+00:00
State: open

Given a server with the following configuration:

```
var connect = require('connect');

var server	= connect.createServer();
var defaultHandler	= connect.static('html/dev');
var appHandler = connect.static('html/dev/app');

function baseRequest(req, res, next) {
console.log(' baseRequest : ' + req.url);
return defaultHandler(req,res,next);
}

function getAppRequestHandler(name) {

return function(req,res,next) {
console.log(name + 'Request: ' + req.url);
return appHandler(req,res,next);
}
}

server.use('/app',	getAppRequestHandler('app'));
server.use('/mary', getAppRequestHandler('mary'));
server.use('/bob', getAppRequestHandler('bob'));
server.use('/',	baseRequest);
server.listen(8080);
```
Accessing the web app using:
http://localhost/bob

results in:

```
bobRequest: /
baseRequest : /css/app.css
baseRequest : /js/lib/jquery-1.6.4.js
baseRequest : /js/lib/jquery.dragsort.js
baseRequest : /js/lib/jquery.globalstylesheet.js
baseRequest : /js/lib/jquery.event.drag-2.0.js
```

Similar results are seen when accessing via app or mary.

However when accessing using (trailing slash):
http://localhost/bob/

The results are as expected:

```
bobRequest: /
bobRequest : /css/app.css
bobRequest : /js/lib/jquery-1.6.4.js
bobRequest : /js/lib/jquery.dragsort.js
bobRequest : /js/lib/jquery.globalstylesheet.js
bobRequest : /js/lib/jquery.event.drag-2.0.js
```

The above example is just to keep things simple. When static middleware is used to serve different directories entirely, unless a trailing slash is appended on to the initial request, the default root (/) ends up servicing the follow on requests.

System: Windows 7 service pack 1
Browser: Chrome 16.0.912.75 m

As an aside, I tested a similar configuration in nginx to make sure it worked as expected:

```
location / {
root html/dev;
index index.html index.htm;
}

location /app {
alias html/dev/app;
index index.html index.htm;
}

location /bob {
alias html/dev/app;
index index.html index.htm;
}

location /mary {
alias html/dev/app;
index index.html index.htm;
}
```

As expected, accessing /bob resulted in the following (with a bit of trimming):

```
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob HTTP/1.1" 301
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob/ HTTP/1.1" 200
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob/css/app.css
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob/js/lib/jquery-1.6.4.js
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob/js/lib/jquery.dragsort.js
127.0.0.1 - - [30/Jan/2012:16:58:13 -0800] "GET /bob/js/lib/jquery.globalstylesheet.js
```