Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

static middleware not working properly #469

Closed
davidcl64 opened this Issue Jan 31, 2012 · 1 comment

Comments

Projects
None yet
2 participants

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
Contributor

jonathanong commented Sep 13, 2013

this issue is kind of dense. what exactly is the problem?

since this is old, i'm closing it. reopen if its a still an issue with the latest version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment