Anil Kumar R
Anil Kumar R

Reputation: 313

Setting headers in hapi.js

I am new to hapi.js, can any one explain me how i can configure custom variables for each route and I should be able to access those custom variables on 'onPreHandler'

And how i can add headers just before calling reply.continue.

Upvotes: 2

Views: 3997

Answers (1)

djelic
djelic

Reputation: 141

I suggest you to read Hapi tutorials, specifically Routing for intro about path parameters:

server.route({
    method: 'GET',
    path: '/hello/{user}',
    handler: function (request, h) {
        return `Hello ${encodeURIComponent(request.params.user)}!`;
    }
});

Same way you can access path parameters in onPreHandler:

server.ext('onRequest', function (request, h) {
  console.log(request.params.user)
  return h.continue;
})

Setting headers can be done like this:

const handler = function (request, h) {
    const response = h.response('success');
    response.header('X-Custom', 'some-value');
    return response;
};

Upvotes: 1

Related Questions