Can I get a clear definition of what the difference between pre() and use() is? It is not entirely clear in the current documentation. Happy to make a PR to the documentation once I get a answer.
To understand the difference, it's important to understand the way requests get routed. Underneath Restify, there is a plain ol' Node core http server. Like all http servers, handling begins with the request event.
When you create a Restify server and mount a route:
server.get('/foo', function handler (req, res, next) {
res.send(200);
});
That registers the handler function[s] in the router. Later, when an incoming request hits the core http server, which fires the request event, and the router finds the matching handler chain. But before the router gets to work, any pre handlers fire first.
It might be helpful to look at issue #64, which was where this method came from.
Thanks @lukealbao for the detailed reply!
In short, pre() handlers are run before the router gets the request. use() and any other handlers registered on a per route basis are run after the router has determined that this a request that can be serviced (and if not, send a 404). We are always happy to take PRs against the documentation!
Most helpful comment
Thanks @lukealbao for the detailed reply!
In short,
pre()handlers are run before the router gets the request.use()and any other handlers registered on a per route basis are run after the router has determined that this a request that can be serviced (and if not, send a 404). We are always happy to take PRs against the documentation!