1

I am running the latest express (4.1.1 as of writing). It has this middleware included to serve static files.

So the usual code to include this middleware is:

app.use(express.static(path.join(__dirname, 'public')));

And great that all works fine. But if I try to include a middleware before that, eg:

app.use(function(req,res,next){                    
    next();                                          
}, express.static(path.join(__dirname, 'public')));

The serve-static middleware now gives me 404s.

I am not sure why this is happening. Did I implement the middleware that goes before the static middleware incorrectly?

user1215653
  • 187
  • 2
  • 2
  • 7

1 Answers1

3

Your use of app.use() is incorrect. From the documentation:

app.use([path], function)
Use the given middleware function, with optional mount path, defaulting to "/".

You will notice that app.use accepts an optional path and a function, not multiple functions. Therefore, you should be defining each middleware with its own app.use call, as seen below:

app.use(function(req,res,next){                    
    next();                                          
});
app.use(express.static(path.join(__dirname, 'public')));