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

I think the express way would be to add a server.use('/api', validate.Credentials). I would prefer this way as the server.use() is already implemented in the new restify version - and the nested routes look a little bit weird ;) All it needs is to add the optional parameter to server.use() to restrict the middleware to specific resources.

fnogatz avatar Jan 09 '13 09:01 fnogatz

I think Falco's suggestion is a nicer API - that gives you the same thing - it'll just be a bit weird to go tack that in in the code, but from a library perspective I like that.

mcavage avatar Jan 09 '13 16:01 mcavage

Thanks for pointing that. Much better that way.

arcmode avatar Jan 10 '13 00:01 arcmode

Reopen as being not implemented yet?

fnogatz avatar Jan 17 '13 22:01 fnogatz

Yes, thanks!.

arcmode avatar Jan 18 '13 17:01 arcmode

+1 for extended use, like in http://expressjs.com/api.html#app.use

jo avatar Apr 30 '13 07:04 jo

+1. I was originally using Express for a large, distributed service and was able to link routes within my sub-modules into my main API's routes like so:

var users = require('module-users');
app.use('/api/users', users);

Being able to do this with Restify would keep me using Restify.

kvnneff avatar Oct 07 '13 14:10 kvnneff

Any updates on this?

Seems like it should be two separate Github issues:

  1. server.use([path], function)
  2. server.all(…)

djensen47 avatar Apr 25 '14 04:04 djensen47

I would love to see the server.use(path, middleware) syntax implemented, as it would enable more modular routes. Had to revert to Express for now.

mieky avatar Apr 28 '15 08:04 mieky

The trouble with supporting this pattern is that if you've already added a route to the route list, this new handler would have to be added to the end or beginning, which could make it too late or too early for the expected behavior. When you are forced to build the handler chain for your route, you can ensure that handlers are executed in the expected order.

micahr avatar Jun 18 '15 04:06 micahr

+1

roblav96 avatar Jun 20 '15 16:06 roblav96

+1

I'm currently using restify-namespace (https://github.com/mpareja/node-restify-namespace) to accomplish similar functionality.

aaronroberson avatar Jun 26 '15 00:06 aaronroberson

Yes I would also like this feature!

fullstackwebdev avatar Jun 27 '15 14:06 fullstackwebdev

Is this still being considered? Im also using restify-namespace for now to check wether restify could replace an express approach. Is there any other mechanic to let's say expose every route under '/api' for example?

Cheers.

erikmueller avatar Nov 27 '15 00:11 erikmueller

What is the status of this issue? It has been suggested 3 years ago!

This is very important because it saves us from doing:

api.use(req,res,next) => {
   if(req.url.startsWith('')){
       //etc
   }
}

sulhome avatar Dec 02 '15 08:12 sulhome

I don't think this will be implemented because it adds too much overhead. I love restify because of how basic and barbones it is. I hope it stays that way.

roblav96 avatar Dec 02 '15 12:12 roblav96

I totally disagree with @roblav96 The case for this feature is justified and indeed very useful. Imagine 5 routes which need the same object (for example book) based on an id from the url. Instead of every route (method) executing the same code to get the same book, you can with this feature supply that book and add it to the req object

sulhome avatar Dec 02 '15 13:12 sulhome

Or do this:

server.post( '/api/public/prekey',
    require( './middle/public/throttle.js' ).bind( envThrottle.pre ),
    require( './middle/public/bytes.js' ),
    require( './routes/public/pre-key.js' )
)
server.post( '/api/public/verifycaptcha',
    require( './middle/public/throttle.js' ).bind( envThrottle.pre ),
    require( './middle/public/bytes.js' ),
    require( './routes/public/verify-captcha.js' )
)
server.post( '/api/public/register',
    require( './middle/public/throttle.js' ).bind( envThrottle.pub ),
    require( './middle/public/bytes.js' ),
    require( './middle/public/verify-prekey.js' ),
    require( './routes/public/register.js' )
)
server.post( '/api/public/login',
    require( './middle/public/throttle.js' ).bind( envThrottle.pub ),
    require( './middle/public/bytes.js' ),
    require( './middle/public/verify-prekey.js' ),
    require( './routes/public/login.js' )
)

It only adds those middlewares to my public routes.

roblav96 avatar Dec 02 '15 13:12 roblav96

We are working on a solution to the issue of route inheritance and sharing of common middleware. You can take a look at: restify/conductor. It is still a work in progress, but is a system we actively use to manage our routes and their dependencies. The biggest benefit we get is to have a set of common route middlewares that we can share across many different routes, without having to resort to adding them to every route manually.

The API is still in flux, but will hopefully firm up soon. The documentation is very minimal right now, but will be improved over time as well.

micahr avatar Dec 02 '15 17:12 micahr

Yes, please check out restify-conductor. It is a different take on the abstraction approach for reusable routes, without explicitly tying them to a specific URL(s).

We would love to hear any feedback you may have.

DonutEspresso avatar Dec 03 '15 03:12 DonutEspresso