Skip to content

Commit 94de951

Browse files
author
Mark Cavage
committed
restify#193 support next("name_of_route")
1 parent 59835d6 commit 94de951

6 files changed

Lines changed: 462 additions & 169 deletions

File tree

‎CHANGES.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# restify Changelog
22

3-
## 2.3.5 (not yet released)
3+
## 2.4.0 (not yet released)
4+
5+
- GH-193 support `next('name_of_route')`
6+
47

58
## 2.3.4
69

‎docs/index.restdown‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,37 @@ You can pass in `false` to not error, but to stop the handler
206206
chain. This is useful if you had a `res.send` in an early filter, which is
207207
not an error, and you possibly have one later you want to short-circuit.
208208

209+
Lastly, you can pass in a string `name` to `next()`, and restify will lookup
210+
that route, and assuming it exists will run the chain *from where you left
211+
off*. So for example:
212+
213+
var count = 0;
214+
215+
server.use(function foo(req, res, next) {
216+
count++;
217+
next();
218+
});
219+
220+
server.get('/foo/:id', function (req, res, next) {
221+
next('foo2');
222+
});
223+
224+
server.get({
225+
name: 'foo2',
226+
path: '/foo/:id'
227+
}, function (req, res, next) {
228+
assert.equal(count, 1);
229+
res.send(200);
230+
next();
231+
});
232+
233+
Note that `foo` only gets run once in that example. A few caveats:
234+
235+
- If you provide a name that doesn't exist, restify will 500 that request.
236+
- Don't be silly and call this in cycles. restify won't check that.
237+
- Lastly, you cannot "chain" `next('route')` calls; you can only delegate the
238+
routing chain once (this is a limitation of the way routes are stored
239+
internally, and may be revisited someday).
209240

210241
### Versioned Routes
211242

‎examples/bench/bench.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var server = require('restify').createServer();
1+
var server = require('../../lib').createServer();
22
//var server = require('express')();
33

44
server.get('/echo/:name', function (req, res, next) {

‎lib/router.js‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var DEF_CT = 'application/octet-stream';
2323
var maxSatisfying = semver.maxSatisfying;
2424

2525
var BadRequestError = errors.BadRequestError;
26+
var InternalError = errors.InternalError;
2627
var InvalidArgumentError = errors.InvalidArgumentError;
2728
var InvalidVersionError = errors.InvalidVersionError;
2829
var MethodNotAllowedError = errors.MethodNotAllowedError;
@@ -258,9 +259,31 @@ Router.prototype.unmount = function unmount(name) {
258259
};
259260

260261

262+
Router.prototype.get = function get(name, req, cb) {
263+
var params;
264+
var route = false;
265+
var routes = this.routes[req.method] || [];
266+
267+
for (var i = 0; i < routes.length; i++) {
268+
if (routes[i].name === name) {
269+
route = routes[i];
270+
try {
271+
params = matchURL(route.path, req);
272+
} catch (e) {}
273+
break;
274+
}
275+
}
276+
277+
if (route) {
278+
cb(null, route, params || {});
279+
} else {
280+
cb(new InternalError());
281+
}
282+
};
283+
284+
261285
Router.prototype.find = function find(req, res, callback) {
262286
var candidates = [];
263-
// var candidateKeys = [];
264287
var ct = req.headers['content-type'] || DEF_CT;
265288
var cacheKey = req.method + req.url + req.version() + ct;
266289
var cacheVal;
@@ -431,7 +454,7 @@ Router.prototype.find = function find(req, res, callback) {
431454
res.methods = this.reverse[urls[i]].slice();
432455
res.setHeader('Allow', res.methods.join(', '));
433456
if (preflight(res.methods)) {
434-
callback(null, true);
457+
callback(null, { name: 'preflight' });
435458
return;
436459
}
437460
var err = new MethodNotAllowedError('%s is not allowed',

0 commit comments

Comments
 (0)