A short nap, which allows for a quick boost in energy.
Catnap is a minimal approach to handle REST Resources on Express-like routers. It simplifies creating named resources and defining their representations while keeping your code DRY.
Catnap doesn't get in your way by imposing an architecture. It can be dropped as-is on existing Express projects.
$ npm install catnap
Catnap lets you create Resources identified by a name and a path. A Resource can have one or many representations and respond to actions (get, post, put, patch an delete.) Here is a contrived example:
var makeResource = require('catnap').makeResource;
var userResource = makeResource('user', '/users/:userId')
.representation(function (user) {
// The default representation. Returns a full representation of user
return user;
})
.representation('partial', function (user) {
// A named representation that returns a partial representation
return pick(user, 'username', 'email');
})
.get(function (req, res) {
// Action methods take standard middleware.
User.findOne({ _id: req.params.userId }, function (err, user) {
user && res.send(200, userResource(user));
});
});
- To get started, check out the Getting Started Guide
- For a concrete example, have a look at Implementing a simple API
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request