groove-js
v0.0.4
Published
A connect router for APIs
Downloads
5
Readme
Groove.js
A connect.js router for APIs.
Usage
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(groove.route({
'GET /ponies.json': poniesController.list
'POST /ponies.json': poniesController.create
}));
Why use Groove.js?
Expressiveness
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,
}));
Flexibility
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.