Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!