A connect.js router for APIs.
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(groove.route({
'GET /ponies.json': poniesController.list
'POST /ponies.json': poniesController.create
}));
When developing APIs your code is usually coupled to the HTTP method, the URL and the querystring. Groove.js let you express that coupling without bloating your handler function.
// Using Express.js
var handler = function(req, res) {
if (! req.query['type']) {
return res.json(find());
}
if (req.query['type'] === 'upcoming') {
if (req.query['cityId']) {
res.json(findByCity(req.query['cityId'], 'upcoming'));
} else {
res.json(find('upcoming'));
}
} else {
res.json(find(req.query['type']));
}
};
// setup express app ...
app.get('/events.json', handler);
// Using Groove.js
var all = function(req, res) { res.json(find()); },
eventsByType = function(req, res) { res.json(find(req.query['type'])); },
upcomingByCity = function(req, res) { res.json(findByCity(req.query['cityId'], 'upcoming')); };
// setup connect app ...
app.use(groove.route({
'GET /events.json?type=upcoming&cityId=:cityId': upcomingByCity,
'GET /events.json?type=:type': eventsByType
'GET /events.json' : all,
}));
Because the input to the router is a simple object mapping URL patterns to functions, Groove.js doesn't impose any rules on how developers should organize their code.