Created
March 27, 2015 09:49
-
-
Save sericaia/6a07e5377a2769118d68 to your computer and use it in GitHub Desktop.
Playing with hapi server.ext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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