Skip to content

Instantly share code, notes, and snippets.

@brianloveswords
Last active July 24, 2019 13:07
Show Gist options
  • Save brianloveswords/9121949 to your computer and use it in GitHub Desktop.
Save brianloveswords/9121949 to your computer and use it in GitHub Desktop.
const restify = require('restify')
const server = restify.createServer({
name: 'next test',
version: '1.0.0'
});
server.get('/', function (req, res, next) {
console.log('nexting')
req.whatever = Date.now()
return next()
})
// This never ends up getting called. I would assume that calling `next` would
// make processing proceed to the next matched handler, but that doesn't appear
// to be the case. What am I doing wrong?
server.get('/', function (req, res, next) {
console.log('done')
res.send('okay ' + req.whatever)
return next()
})
server.listen(8000)
@brianloveswords
Copy link
Author

As mentioned in the code comment, I would expect that GET / would log "nexting" to the console, then log "done" to the console, then send "okay " to the client.

What happens in practice is that I get "nexting" in the console and nothing else.

@thlorenz
Copy link

not entirely sure, but afaik restify plugins !== express middleware.
So not surprised that it behaves differently.

Also I'd expect one route to overwrite the other since they have exact same string.
You could throw my routes thing at it to see what got registered.

Something like:

var renderRoutes = require('render-routes');

module.exports =

/**
 * Route /routes sends a visualization of all routes registered with the app router.
 *
 * @param {Object} app     restify app
 * @param {Object} restify restify module
 */
function routes(app, restify) {
  app.get('/routes', function (req, res) {
    res.header('ContentType', 'text/html');
    res.end(renderRoutes(app.router.routes));
  });
};

@brianloveswords
Copy link
Author

AH, okay, figured this out, it needs to be expressed like this:

const restify = require('restify')

const server = restify.createServer({
  name: 'next test',
  version: '1.0.0'
});

server.get('/', [
  function (req, res, next) {
    console.log('nexting')
    req.whatever = Date.now()
    return next()
  },
  function (req, res, next) {
    console.log('done')
    res.send('okay ' + req.whatever)
    return next()
  }
])

server.listen(8000)

A little bit annoying if you want to have some common functionality based on a common prefix, though (requires a lot of duplication)

@thlorenz
Copy link

Ah, interesting, that's kinda like express per route middleware actually.
Worth blogging about it ;)

Have you looked at use? That may work better for what you are trying to do.

app.use('/', mw1);
app.use('/', mw2);

@brianloveswords
Copy link
Author

I tried, but use doesn't want to work like that:

± node test-server.js

/Users/brian/projects/badgekit-issue/node_modules/restify/node_modules/assert-plus/assert.js:44
                        throw new assert.AssertionError({
                              ^
AssertionError: handler (function) is required
    at process (/Users/brian/projects/badgekit-issue/node_modules/restify/lib/server.js:67:40)
    at argumentsToChain (/Users/brian/projects/badgekit-issue/node_modules/restify/lib/server.js:75:17)
    at Server.use (/Users/brian/projects/badgekit-issue/node_modules/restify/lib/server.js:514:10)
    at Object.<anonymous> (/Users/brian/projects/badgekit-issue/test-server.js:8:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

@ethyaan
Copy link

ethyaan commented Oct 24, 2016

any update or solution this people?
I have same problem in mac os sierra.
/Users/ehsan/Sites/parsefolder-BE/node_modules/assert-plus/assert.js:45
`

throw new assert.AssertionError({
^AssertionError: handler (function) is required
at process (/Users/ehsan/Sites/parsefolder-BE/node_modules/restify/lib/server.js:76:24)
at argumentsToChain (/Users/ehsan/Sites/parsefolder-BE/node_modules/restify/lib/server.js:84:13)
at Server.(anonymous function) [as post] (/Users/ehsan/Sites/parsefolder-BE/node_modules/restify/lib/server.js:503:13)
at new usersController (/Users/ehsan/Sites/parsefolder-BE/application/controllers/users.js:23:16)
at module.exports (/Users/ehsan/Sites/parsefolder-BE/application/controllers/users.js:138:30)
at module.exports (/Users/ehsan/Sites/parsefolder-BE/application/controllers/main.js:3:23)
at module.exports (/Users/ehsan/Sites/parsefolder-BE/application/load.js:9:34)
at Object.<anonymous> (/Users/ehsan/Sites/parsefolder-BE/app.js:46:34)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)

Worker 16252 is online`

@pimvanderheijden
Copy link

Any news on this?

@igalep
Copy link

igalep commented Jan 18, 2018

same issue as well

    server.use('/foo' , _ProcessRequest)
    function _ProcessRequest(req, res , next){...}

getting "AssertionError: handler (function) is required"

@remgi
Copy link

remgi commented Jan 16, 2019

same issue macos high sierra version 10.13.6

@RahulSaini202
Copy link

same issue arise at my end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment