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

143
Views
Koa.js retain state between requests

I am trying to post some data in Koa.js, the post is working fine, but I am struggling to show errors when the form is not filled in correctly. The site needs to work without javascript so I can't guarantee the fields will be correct before submitting (though I have implemented this).

Everytime I redirect back to the form the ctx.request.body and ctx.state are empty.

The only way I've been able to get the data back to the original form is using query params. Fairly new to Koa.js but I've managed to do similar things with Express before.


router.post('/admin/addChapter', (ctx) => {
    const body = ctx.request.body;
    const expectedKeys = requiredInputs.addChapter;
    const hasAll = expectedKeys.every(key => Reflect.has(body, key));

    if (hasAll) {
        return ctx.render('admin/addChapter', {
            seoDescription: 'Some Seo',
            pageTitle: 'Add chapter',
            titleParam: 'Add chapter',
            scripts: ['addChapter/index']
        });
    } else {
        let urlStr = Object.entries(body).map((entry) => entry.join('=')).join('&');
        urlStr = urlStr ? `?${urlStr}` : '';

        ctx.redirect(`/admin/add${urlStr}`);
        return;
    }
});
about 4 years ago · Juan Pablo Isaza
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!