May 2018
Intermediate to advanced
492 pages
12h 3m
English
We've seen two kinds of middleware functions so far. In one, the first argument is the handler function. In the other, the first argument is a string containing a URL snippet, and the second argument is the handler function.
What's actually going on is app.use has an optional first argument: the path the middleware is mounted on. The path is a pattern match against the request URL, and the given function is triggered if the URL matches the pattern. There's even a method to supply named parameters in the URL:
app.use('/user/profile/:id', function(req, res, next) { userProfiles.lookup(req.params.id, (err, profile) => { if (err) return next(err); // do something with the profile // Such as display it to the user ...Read now
Unlock full access