node-restify icon indicating copy to clipboard operation
node-restify copied to clipboard

Support server.use('/route', handler) and server.all()

Open arcmode opened this issue 13 years ago • 39 comments

Edited

Would be very nice to have server.use('/route', handler) and server.all()functions in restify.

Thanks Falco and Mark.

Cheers, David

arcmode avatar Jan 09 '13 02:01 arcmode

+1

omnip620 avatar Dec 11 '15 07:12 omnip620

+1

caomu avatar Dec 11 '15 08:12 caomu

I started converting everything to restify this morning and it went really well until i hit this roadblock. We were previously using the middleware solution for route inheritance as well in express.

michaelfavia avatar Dec 16 '15 17:12 michaelfavia

+1000 This is required to do some common logic, please add

CreativeManix avatar Jan 22 '16 10:01 CreativeManix

If you're working with simple middleware, you might be able to get away with this 8 line replacement for the server.use('/prefix', middleware) signature:

function scopeMiddlewareTo(prefix, middleware) {
  return function(req, res, next) {
    if (req.url.indexOf(prefix) === 0) {
      req.url = req.url.slice(prefix.length);
      return middleware.call(this, req, res, next);
    }
    else { return next(); }
  };
}

And use with:

server.use(scopeMiddlewareTo('/prefix', myMiddleware));

This works for very simple middleware that are only checking the req.url, but hey that's most cases, and will be easier to add than getting Restify to change or opt'ing for the overkill Restify conductor.

tbranyen avatar Mar 28 '16 15:03 tbranyen

bump :thinking:

scippio avatar Oct 24 '16 12:10 scippio

@tbranyen Why do you slice the req.url? If you wish to have more than one handler on the same prefix, this slice makes that impossible.

Good workaround though, thanks.

FrikkieRetro avatar Nov 29 '16 09:11 FrikkieRetro

+1 Has this been implemented yet? Is anyone working on it?

saidkholov avatar Dec 15 '16 02:12 saidkholov

Maybe this will help https://www.npmjs.com/package/restify-router

cocoya23 avatar Feb 16 '17 19:02 cocoya23

+1

mwthink avatar Feb 22 '17 05:02 mwthink

+1

chrisenytc avatar Mar 25 '17 00:03 chrisenytc

This is essential, especially when using versioning via GET instead of headers and with many routes.

flowl avatar Mar 30 '17 07:03 flowl

+1 - would be great to have for shared middleware used across versioned routes

mossy02 avatar Apr 06 '17 18:04 mossy02

+1 - will this feature be added in v5.x ?

minhchu avatar Apr 13 '17 12:04 minhchu

@tbranyen https://github.com/restify/node-restify/issues/289#issuecomment-202445094

This doesn't work, since, restify only routes through middleware when it knows about a registered route on the prefix. You will get ResourceNotFound if the requested http verb on the route isn't found.

alber70g avatar Apr 19 '17 20:04 alber70g

@Alber70g absolutely correct, that's something that has bitten me many times using Restify. I've since abandoned Restify in favor of Express, but our team would like that to be a choice. I met with current maintainers of Restify @DonutEspresso and @yunong and we all agreed this feature should be implemented. However, we need to spec out the work and figure out a less ad-hoc way of introducing the change.

The work here would need to include (at a minimum):

  • Public Router API
  • Allowing use to accept a prefix
  • Routing changes to account for unknown resources
  • Documentation changes

This weekend I'll put together an idea of what could be implemented and mock out the intended behavior. If anyone else wants to help contribute to this effort, please think of potential gotchas and wants along with implementation concerns.

tbranyen avatar Apr 19 '17 22:04 tbranyen

@tbranyen I'm not sure, but if you'd use a regex on a get,post,put,delete it would trigger all middleware since the regex would potentially match on all requests. Although I think there's a reason why this shouldn't work, as it gives a huge performance penalty on routes that don't exist (probably?).

Maybe that's why we wouldn't want it in this way. A structural solution for scoped routes would be better. Maybe all routes nested under a server.use(<prefix>, middleware), should be 'registered' to the pipeline explicitly (e.g. api/ -users -posts -things would be registered as api/users api/posts api/things). In a way server.use(<prefix>, middleware) becomes then more of syntactic sugar

alber70g avatar Apr 23 '17 15:04 alber70g

There is https://www.npmjs.com/package/restify-router to organize routes/subroutes. Not sure if this works for middlewares.

MartinMuzatko avatar Jun 04 '19 12:06 MartinMuzatko

https://www.npmjs.com/package/restify-prefix-route

Use this to add prefix in all routes like... server.pre(applyPrefix('/v1'));

abhikk avatar May 20 '22 19:05 abhikk