سؤال

I'm using node-restify and am trying to overload a GET route - is this possible? Shouldn't next() call the next matching route registered?

Here is an example. Any hints as to why it wouldn't work?

server.get "search", (req, res, next) ->
    query = req.params.q
    console.log 'first handler'
    return next() if not query?

    # implement search functionality... return results as searchResults

    res.send 200, searchResults
    next()

server.get "search", (req, res, next) ->
    console.log 'second handler'
    res.send 200, "foo"
    next()

I'd expect /search to output "foo", and I'd expect /search?q=bar to output all records matching the "bar" search term.

هل كانت مفيدة؟

المحلول 2

@robertklep was close - you should just add a name to the route.

The express "route chain" syntax isn't supported, but the same functionality can be accomplished like this:

server.get('/foo', function (req, res, next) {
  if (something()) {
    next('GetBar');
    return;
  }

  res.send(200);
  next();
});

server.get({
  path: '/bar',
  name: 'GetBar'
}, function (req, res, next) {
  res.send(200, {bar: 'baz'));
  next();
});

https://github.com/mcavage/node-restify/issues/193

نصائح أخرى

I'm not very familiar with Restify, but it certainly works differently than Express.

I got it to work using this:

app.get('/search', function(req, res, next) {
  var q = req.params.q;
  if (! q) {
    return next('getsearchfallback');
  }
  res.send('bar');
});

app.get('search-fallback', function(req, res, next) {
  res.send('foo');
  next();
});

I'm not sure if that's how things should be done, though.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top