When using Express I can define routes with a placeholder in the route string, something like:
app.get("/users/:user_id/photos", function(req,res){<blah>});
and then in my handler I can user req.params["user_id"] to get whatever was in the URL and use it in my request.
Middleware can be mounted at certain paths such that only requests matching that path will use the middleware. Can I use placeholders in the mount path of a middleware? For example, could I do something like:
app.use("/users/:user_id/photos", <middleware>);
and then inside the middleware have some way of accessing what the segment that maps to :user_id was?
EDIT 1:
I am aware that I can put the middleware directly in the route declaration, à la:
app.get("/users/:user_id/photos", <middleware>, function(req,res){<blah>});
It doesn't take much imagination to see how that would get out of hand as an app grows.
Middlewares are chaining in the order you add them.
middleware = function(req,res,next){
if(valid(req))
next();
else
res.send(400, "Emergerd");
}
// First middleware
app.get("/users/:user_id/photos", middleware);
app.get("/users/:user_id/photos", function(req,res){
// function after middleware
});