Forked from sericaia/hapi-server-ext-route-validation
Created
December 7, 2017 11:30
-
-
Save carlozamagni/e7ef22b1720ef4d1be9dd035599da180 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