I would like to implement X-Authorization to my routes in Hapi.js. So when I make a request, I will create a X-Auth header and I would like Hapi to check it before allowing some code execution:
Based on this code example from Hapi documentaiton, how Do I implement it?
server.route({
method: 'GET',
path: '/hello/{user?}',
handler: function (request, reply) {
const user = request.params.user ? encodeURIComponent(request.params.user) : 'stranger';
reply('Hello ' + user + '!');
},
config: {
description: 'Say hello!',
notes: 'The user parameter defaults to \'stranger\' if unspecified',
tags: ['api', 'greeting']
}
});
You can use the headers
property of the request
object to get the X-Authorization
value. However, it should be request.headers['x-authorization']
(all lowercase)[1].
The request header name should be in lowercase because in Node.js, the request
object belong to the http.IncomingMessage
class, whose header names are in lowercase.[2]
[1] http://hapijs.com/api#request-properties
[2] https://nodejs.org/dist/latest-v4.x/docs/api/http.html#http_message_headers