Search code examples
node.jscookieshttp-headershapi.jshttpcookie

How to get Cookies in Nodejs/Hapijs


I am new to NodeJs Development. I am using HapiJs framework. I have set cookie using following code:

        reply("hello").state('cookie_name', accessToken, {
          ttl: 365 * 24 * 60 * 60 * 1000, // expires a year from today
          encoding: 'none',
          isSecure: false,
          isHttpOnly: false,
          clearInvalid: false,
          strictHeader: true
        });

the above code is working perfectly and I am able to see the cookie being set in browser with the name cookie_name along with an extra cookie named io (which I guess is automatically set by socket.io node module I am using)

but when I am trying to get that cookie using following code

request.headers.cookie

It only shows one cookie (i.e io). its not showing me up the cookie with the name cookie_name. So, can anyone tell me what is the mistake am I making here. Am I passing incorrect values in options while setting cookie or do I need to retrive cookies using some other way.


Solution

  • In our Hapi application, we're using the hapi-auth-cookie plugin.

    There is an example application here that shows its use quite well.