named-routes icon indicating copy to clipboard operation
named-routes copied to clipboard

Argument order

Open damianstasik opened this issue 7 years ago • 1 comments

If you are using this module with Express, you can create a named route like that:

app.get('/user/:id', 'user', (req, res) => {
    res.send('Hello!');
});

It's clean and neat, but is you are using standalone mode and want to create the exact route, you end up with that:

router.add('get', '/user/:id', (req, res) => {
    res.send('Hello!');
}, { name: 'user' });

It feels bloated, but wait, what if you want some middlewares?

router.add('get', '/user/:id', [isAuthenticated, someOtherMiddleware, (req, res) => {
    res.send('Hello!');
}], { name: 'user', caseSensitive: true });

My proposal would be to:

  • [ ] Change order of the options argument, so it would be after the path.
  • [ ] Allow options to be string, then it would be used as a route name.
  • [ ] Use the arguments object, so if there would be more than one middleware, then putting them in an array would be optional, like in Express.
  • [ ] If the options argument would be a callback, then instead of slicing three args from the arguments object, it would slice two, so options would be optional.

All of the changes above would allow to create a named route like that:

router.add('get', '/user/:id', 'user', isAuthenticated, someOtherMiddleware, (req, res) => {
    res.send('Hello!');
});

router.add('get', '/post/:id', (req, res) => {
    res.send('Hello!');
});

I can create a pull request, but first I would love to know your opinions.

damianstasik avatar Aug 15 '16 15:08 damianstasik

Yes, the standalone version has been lagging behind the express integration in usability. I like your approach and am looking forward to your PR!

alubbe avatar Aug 19 '16 16:08 alubbe