1
var s_bookingController = require('s/controllers);

app.get('/dashboard/:page/:param', s_bookingController.index)
app.get('/dashboard/show/:id', s_bookingController.show);

Controllers:

exports.index = function(req, res, next) {
    var page = parseInt(req.param("id"));

    data = {};
    data.page = page;
    data.nextPage = page + 1;
    data.prevPage = page - 1;

    MyModel.find().sort('brand').skip((page-1)*11).limit(11).exec(function(err, result) {
        res.render('index', {
            data: data,
            booking: result,
        });
    });
};

And

exports.show = function(req, res, next) {
    var id = req.param("id");

    res.send(id);
};

I'm using this controllers, but there is something wrong with the code of the exports.index, because it's stuck in the code.

If I change the routes to:

app.get('/dashboard/:page', s_bookingController.index)

(Note that I'm take off the second parameter that I was passing)

the show will work, but if I use the second parameter, the show will not run, it will be stuck in the index page.

Why is this? I was wondering if I need use the next();.

laggingreflex
  • 32,948
  • 35
  • 141
  • 196

1 Answers1

0

Expanding my comment:

You should have the following order of the routes:

app.get('/dashboard/show/:id', s_bookingController.show);
app.get('/dashboard/:page/:param', s_bookingController.index);

Express routing requires that a more specific route should be placed above the more general one.

The /dashboard/show/:id is more specific in this case as /dashboard/:page/:param covers it, so that /dashboard/show is handled by it. When the route is handled next routes are not executed.

Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
  • I understood.. but, what are the criteria?? Why the show is more specific @VsevolodGoloviznin ?? –  Jan 12 '15 at 23:22