Skip to content

Instantly share code, notes, and snippets.

@sericaia
Created March 27, 2015 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sericaia/6a07e5377a2769118d68 to your computer and use it in GitHub Desktop.
Save sericaia/6a07e5377a2769118d68 to your computer and use it in GitHub Desktop.
Playing with hapi server.ext
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
server.ext('onRequest', function (request, reply) {
//perform route validations
routeValidation(encodeURIComponent(request.params.userId), function(err, result){
return (err || !result) ? reply(err) : reply.continue();
});
});
server.route({
method: 'GET',
path:'/user/{userId}',
handler: function (request, reply) {
reply('iupi! ' + encodeURIComponent(request.params.userId));
}
});
server.start();
var routeValidation = function(userId, callback){
//perform some kind of validation
// ...
callback(null, userId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment