connect-route
connect-route copied to clipboard
Missing catch all http methods function
trafficstars
router.all('/path', function(req) {
console.log('Hello world');
});
This may feel like a feature completing the interface, but I am not sure, if it has a real benefit in concrete applications. Handlers for different methods like GET and POST tend to need a different code too. What is your use case for the all handler?
I've actually found one: adding members to req or res objects, if you want to reuse them in multiple requests.
router.all('/api/*rest', function (req, res, next) {
res.sendData = function (...) {...};
res.sendError = function (...) {...};
next();
})
.get('/api/pets', function (req, res, next) {
res.sendData([...]);
})
.put('/api/pet/:id', function (req, res, next) {
var pet = pets[req.params.id];
if (pet) {
...
res.sendData(...);
} else {
res.sendError(...);
}
});
Another solution for performing shared code from my comment above would be allowing multiple handlers (#17). It would be probably better, because .the URL would not need to be parsed twice.