Node.js - Kraken.js - Error in router callbacks

634 Views Asked by At

I'm stuck on a what-should-be-simple problem for hours now, and similar questions did not help me =/

I'm using kraken.js because it has all the features I need out of the box and I've got a problem with my routes : Error: Route.get() requires callback functions but got a [object Undefined] .

So, in my config.json I set up my router to this : "router": { "module": { "arguments": [{ "index": "path:./routes" }] } }

and my routes.js looks like this :

'use strict';

module.exports = function (router) {
  var controllers = require('./controllers');

  router.get('/', controllers.index);
  //router.get('/offer', controllers.offer);
  //router.get('/specifications', controllers.specifications);

  router.get('/setLocale/:locale', function (req, res) {
    res.cookie('locale', req.params.locale);
    res.redirect('/');
  });
};

I have this architecture :

[ROOT]
  |-> controllers
      |-> index.js
      |-> offer.js
      |-> specifications.js
  |-> routes.js
  |-> models
      |-> index.js
      |-> offer.js
      |-> specifications.js
  |-> [some other folders and files]

Here are my controllers :

index.js :

'use strict';

var IndexModel = require('../models/index');

exports.index = function(req, res) {
  var model = new IndexModel();
  res.render('index', model);
};

offer.js :

'use strict';

  var OfferModel = require('../models/offer');

  exports.offer = function(req, res) {
    var model = new OfferModel();
    res.render('offer', model);
};

specifications.js :

'use strict';

var SpecificationsModel = require('../models/specifications');

exports.specifications = function(req, res) {
  var model = new SpecificationsModel();
  res.render('specifications', model);
};

So as you can see, the 3 controllers respect the same format but only the controller.index callback works. When I uncomment the controller.[order|specifications] lines it give me an error for calling an undefined object instead of a function, why is that ?

Also, I tried adding module. before each export in my controllers and the error is gone (which may seems to be a good thing), but I then tells me that the routes do not link to any files ...

If anyone could help, that would be so great.

1

There are 1 best solutions below

0
On BEST ANSWER

I found an answer here. I cannot require multiple files in only one require statement. So I added a controllers/controllers.js file with :

'use strict';

module.exports= {
  index : require('./index'),
  offer : require('./offer'),
  setLocale : require('./setLocale'),
  specifications: require('./specifications')
};

and in my routes.js I can load them by requiring ./controllers/controllers.js

Now I have to find why the routes work but the pages are empty ... Anyway, that's another problem. Thx Kilian.