0
votes

I created a Node.js project using Express and got this exception when using a customized routes.

500 TypeError: fn is not a function at callbacks (/WallaceBot/WallaceBot/node_modules/express/lib/router/index.js:272:11) at param (/WallaceBot/WallaceBot/node_modules/express/lib/router/index.js:246:11) at pass (/WallaceBot/WallaceBot/node_modules/express/lib/router/index.js:253:5) at Router._dispatch (/WallaceBot/WallaceBot/node_modules/express/lib/router/index.js:280:5) at Object.Router.middleware [as handle] (/WallaceBot/WallaceBot/node_modules/express/lib/router/index.js:45:10) at next (/WallaceBot/WallaceBot/node_modules/express/node_modules/connect/lib/http.js:204:15) at Object.methodOverride [as handle] (/WallaceBot/WallaceBot/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5) at next (/WallaceBot/WallaceBot/node_modules/express/node_modules/connect/lib/http.js:204:15) at Object.bodyParser [as handle] (/WallaceBot/WallaceBot/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:88:61) at next (/WallaceBot/WallaceBot/node_modules/express/node_modules/connect/lib/http.js:204:15)

And I declare the routes in app.js by

var webhook = require('./routes/webhook.js');
app.get('/', routes.index);
app.get('/webhook', webhook);

And in my webhook.js,

/*
 * GET Webhook.
 */

exports.webhook = function(req, res){
  res.render('index', { title: 'Webhook' })
};

However, I use another way to declare the route in app.js, like

app.get('/webhook', function(req, res){
  res.render('index', { title: 'Webhook' })
});

I don't get that exception.

Does anybody know why?

2

2 Answers

1
votes

As an alternative solution to the other answer you may change your webhook.js file to look like this:

/*
 * GET Webhook.
 */

exports = module.exports = function(req, res){
  res.render('index', { title: 'Webhook' })
};
3
votes

var webhook looks like this:

{
  "webhook" : function(req, res) { ... }
}

So your route handler setup looks like this:

app.get('/webhook', {
  "webhook" : function(req, res) { ... }
});

Which is invalid, because Express wants a function argument, not an object.

Instead, you want to use webhook property of the exported module object:

var webhook = require('./routes/webhook.js').webhook;