Support server.use('/route', handler) and server.all()
Edited
Would be very nice to have server.use('/route', handler) and server.all()functions in restify.
Thanks Falco and Mark.
Cheers, David
+1
+1
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.
+1000 This is required to do some common logic, please add
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.
bump :thinking:
@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.
+1 Has this been implemented yet? Is anyone working on it?
Maybe this will help https://www.npmjs.com/package/restify-router
+1
+1
This is essential, especially when using versioning via GET instead of headers and with many routes.
+1 - would be great to have for shared middleware used across versioned routes
+1 - will this feature be added in v5.x ?
@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 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
useto 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 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
There is https://www.npmjs.com/package/restify-router to organize routes/subroutes. Not sure if this works for middlewares.
https://www.npmjs.com/package/restify-prefix-route
Use this to add prefix in all routes like... server.pre(applyPrefix('/v1'));