Search code examples
javascriptexpressvalidationmiddlewarestatic-files

express static routes are not working when adding a validation middleware


I am trying to add a validation middleware in order to protect my server data. When I get request (http://localhost:3000/filepath) the static route without the middleware:

app.use(express.static('data'));

I get a status 200 OK. But when I tried to get request the same route, but this time using a simple middleware as recommended in this other question (Is it possible to use validation with express static routes?)

var staticMiddleware = function(req, res, next){
    console.log("middleware")
    next();
}

app.use(staticMiddleware, express.static('data'));

I`ve got a status 404 not found.

How can I add another middleware to app.use before the express.static middleware?


Solution

  • You need to add them seperately.

    app.use(express.static('data'));
    
    var staticMiddleware = function(req, res, next){
        console.log("middleware")
        next();
    }
    
    app.use(staticMiddleware );