https://github.com/opentable/hapi-methods
helper for registering cached server methods
https://github.com/opentable/hapi-methods
Last synced: 3 months ago
JSON representation
helper for registering cached server methods
- Host: GitHub
- URL: https://github.com/opentable/hapi-methods
- Owner: opentable
- License: mit
- Created: 2014-09-10T16:25:35.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-04-08T11:32:35.000Z (about 9 years ago)
- Last Synced: 2025-03-02T15:18:07.586Z (3 months ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 20
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#hapi-methods
[](https://travis-ci.org/opentable/hapi-methods) [](http://badge.fury.io/js/hapi-methods) My narrow helper for server-methods, use it, or don't. Assumes that you are caching your method results.
Benefits:
- does a request.log(["cachemiss"], ...) so you can track the hit/miss ratio
- generates a sha1 key instead of stringifying the input args (useful if you have large input objects)
- what more do you need?```
var catbox-redis = require("catbox-redis");
var Hapi = require("hapi");
var server = new Hapi.Server({
cache: {
engine: require("catbox-redis"),
host: 127.0.0.1
}
});server.connection({ port: 3000 });
var methods = require("hapi-methods");
var config = {
expiryInSeconds: 60
};var methods = {
add: function(a, b, next){
next(null, a+b);
}
};methods.register(server, config, methods);
server.route({
method: 'GET',
path: '/add/{one}/{two}'
handler: function(req, res){
req.server.methods.add(req.params.one, req.params.two, req, function(err, result){
if(err){
reply(err);
}reply(result);
});
}
});server.start(function(){
console.log('server started...');
});```