I have two GET routes for get stores but, one route is for get all stores and the other route is for get just nearby stores.
1) The url request for get all stores is as follows:
http://mydomain/stores
2) The url for get all nearby stores:
http://mydomain/stores?lat={lat}&lng={lng}&radius={radius}
The question is:
How can I map those urls properly in Express, in a way to redirect each route to the corresponding method?
app.get('/stores', store.getAll);
app.get('/stores', store.getNear);
app.get('/stores', function(req, res, next){
if(req.query['lat'] && req.query['lng'] && req.query['radius']){
store.getNear(req, res, next);
} else {
store.getAll(req, res, next)
};
});
edit - a second way to do it:
store.getNear = function(req, res, next){
if(req.query['lat'] && req.query['lng'] && req.query['radius']){
// do whatever it is you usually do in getNear
} else { // proceed to the next matching routing function
next()
};
}
store.getAll = function(req, res, next){
// do whatever you usually do in getAll
}
app.get('/stores', store.getNear, store.getAll)
// equivalent:
// app.get('/stores', store.getNear)
// app.get('/stores', store.getAll)