connect-route icon indicating copy to clipboard operation
connect-route copied to clipboard

Missing catch all http methods function

Open moander opened this issue 10 years ago • 3 comments
trafficstars

router.all('/path', function(req) {
  console.log('Hello world');
});

moander avatar Jul 10 '15 17:07 moander

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?

prantlf avatar Jul 09 '17 14:07 prantlf

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(...);
        }
      });

prantlf avatar Jul 15 '17 17:07 prantlf

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.

prantlf avatar Oct 05 '17 08:10 prantlf