Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
Hapijs - add cookie to all requests

Using the Hapijs Node framework, I want to ensure a certain cookie exists for each request. If it doesn't exist, I want to create it. I'd like to do this without manually adding that cookie to each reply. For example, something like this:

server.ext('onPreHandler', function (request, reply) {
    console.log(`state: ${JSON.stringify(request.state)}`) // state == null
    request.state['my_cookie'] = request.state.my_cookie || 'my data'
    return reply.continue();
});


server.route({
    method: 'GET',
    path: '/',
    handler: function(request, reply) {
        // includes `my_cookie`
        reply(`Cookies: ${JSON.stringify(request.state)}`);
    }
})

// cookie does not exist in the browser

Adding that cookie to the reply works, but will require adding to every reply across the app.

server.route({
    method: 'GET',
    path: '/',
    handler: function(request, reply) {
        reply(`Cookies: ${JSON.stringify(request.state)}`).state('my_cookie', 'my_data');
    }
})

// cookie now exists in the browser

How can I ensure that a cookie is added to all requests, without manually adding a cookie to each reply?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

hapi provides you with the functionality to use cookies out of the box.

At first, you need to prepare your server for a specific cookie:

const Hapi = require('hapi')  
// create new server instance
const server = new Hapi.Server()

server.state('session', {  
  ttl: 1000 * 60 * 60 * 24,    // 1 day lifetime
  encoding: 'base64json'       // cookie data is JSON-stringified and Base64 encoded
})

Afterwards, you can set your cookies using the reply interface. This can be in a route handler or a plugin extending the request lifecycle.

Extend the request lifecylce (plugins are a good use-case for that)

The sample code below extends hapi’s request lifecycle and with that is processed on every request

server.ext('onRequest', function (request, reply) {
  if (!request.state.cookie_name) {
    reply.state('session', {your: 'cookie_data'})
  }

  reply.continue()
})

Route handler

server.route({  
  method: 'GET',
  path: '/',
  config: {
    handler: function (request, reply) {
      const cookie = {
        lastVisit: Date.now()
      }

      reply('Hello Future Studio').state('session', cookie)
    }
  }
})

Read the cookie like this:

const cookie = request.state.session

You can read more on cookies and how to keep states across requests, in this guide.

Hope that helps!

over 4 years ago · Santiago Trujillo Denunciar

0

I was ultimately able to do this with server.ext.

server.ext('onPreHandler', function (request, reply) {
    request.state.my_cookie || reply.state('my_cookie', 'my value')

    reply.continue();
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda