imrefazekas / connect-rest

Exceptionally featureful Restful web services middleware for connect node.js
MIT License
100 stars 29 forks source link

Having a problem with path resolution #21

Closed jwvanderbeck closed 9 years ago

jwvanderbeck commented 9 years ago

I have the following snippet of code:

rest.get( { path : '/ships', unprotected: true }, function(req, content, cb){
    Ship.find({}, function(err, ships){
        if (err) return cb({error:'Internal Error'});
        // cb(null, ships);
        cb(null, ships.map(function(ele) {
            return {
                '_id' : ele._id,
                'name' : ele.name,
                'displayName' : ele.displayname,
                'shipManufactor' : ele.shipManufacturer,
                'enabledInSSB' : ele.enabledInSSB,
                'deprecated' : ele.deprecated
            };
        }));
    });
});
rest.get( { path : '/ships/id/:id', unprotected : true }, function(req, content, callback){
    console.log("Looking for ship ID " + req.params.id);
    Ship.findById(req.params.id, function(err, ship){
        if (err) {
            if (err.name == 'CastError') {
                var error = new Error("Unable to parse given ID -- Check that the given ID is properly formatted and the correct number of characters");
                error.statusCode = 500;
                return callback(error, {'result' : err});
            }
            var error = new Error("An internal database error occured");
            error.statusCode = 500;
            return callback(error, {'result' : err});
        }
        if (ship === null) {
            var error = new Error('Document not found');
            error.statusCode = 404;
            return callback(error);
        }
        else {
            callback(null, ship);
        }
    });
});

Defining two paths, one "/ships" and one "/ships/id/:id" - yet whenever I call just plain "/ships" it is the SECOND one, "/ships/id/:id" that is firing! Why would this be?

jwvanderbeck commented 9 years ago

On further examination it appears to be rolling over from one get to the next, to the next, to the next - in other words it IS matching the first, but then it is also matching the more specific ones following it. Maybe this is just a misunderstanding on my part of how to arrange my paths. I thought it would stop at the first match. I'm going to try arranging things from MOST specific to least and see if that works.

jwvanderbeck commented 9 years ago

Ok that didn't fix it. It still tries to match on /ships/id/ instead of /ships

I'm very confused now.

imrefazekas commented 9 years ago

Hello,

Thank you for reporting this. I added tests for such cases and actually both handlers have been fired. Please use the latest version I just published, it will fix your issue nicely. @1.4.7

Thank again!