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

287
Vistas
Koa server-- How to pass data in the server between middlewares so I can pass it to the front end

In my server, I have the following set up to grab the current logged-in user data:

  const server = new Koa();
  const router = new Router();
  server.keys = [Shopify.Context.API_SECRET_KEY];
  server.use(
    createShopifyAuth({
      async afterAuth(ctx) {
        // Access token and shop available in ctx.state.shopify
        const { shop, accessToken, scope } = ctx.state.shopify;

  const client = new Shopify.Clients.Rest(shop, accessToken);
        const data = await client.get({
          path: 'users/current',
        });

I am correctly getting the data but I would like to pass it to the front end. I've tried storing the data in a global variable, I've tried storing the data in app.context and I've tried storing the data in ctx.state.

My idea is that I'd be able to grab the data and place it in the following so that I can make a fetch request to this /user endpoint:

router.get('/user', (ctx,next) => {
  next();
  console.log('check for ctx.state: ', ctx.state);
})
  

At the end of the day, I need access to the shop and accessToken from the Auth function which is why I'd need to pass the data instead of making the data request in the router.get function. I haven't worked with Koa before so any help would be greatly appreciated. Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I figured this out in case this may be helpful to anyone else:

within the server.use function, I grabbed the data and stored it as the following:

server.context.db = data;

This context allows you to pass it around to other middlewares throughout the app. I then assigned ctx.body = ctx.db.body in my router so that I could use a fetch request to hit that endpoint

about 4 years ago · Juan Pablo Isaza 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