Cannot use basic authentication while serving static files

859 views
Skip to first unread message

Brad Cavanagh

unread,
Jan 16, 2014, 2:23:00 PM1/16/14
to expre...@googlegroups.com
I'm trying to serve up static files contained in a directory while also putting basic authentication on it. When I do so, I am prompted for the username and password (as expected), but after I correctly enter them, I'm given a 404. If I remove the authentication check and serve up the same directory, I actually get the pages contained within it.

Here's a quick server I whipped up as a proof-of-concept:

  var express = require('express');

  var app = express();

  var auth = express.basicAuth(function(user,pass) {
    return 'user' === user && 'pass' === pass;
  });

  app.use('/static', auth, express.static(__dirname + '/static'));
  app.use('/staticNoAuth', express.static(__dirname + '/static'));

  app.listen(8082);


When I hit localhost:8082/admin I get the username/password, then a 404. When I hit localhost:8082/adminNoAuth it serves up the page.

I'm using Express 3.4.7 and node.js 0.10.22. Anybody have any ideas how I can do what I'm trying?

Cheers,

 Brad.

greelgorke

unread,
Jan 17, 2014, 4:15:22 AM1/17/14
to expre...@googlegroups.com
app.use does not support this approach of chaining middleware. try this:

app.use('/static', auth);
app.use('/static', express.static(__dirname + '/static'));
app
.use('/staticNoAuth', express.static(__dirname + '/static'));
Reply all
Reply to author
Forward
0 new messages