Questions tagged [koa2]

For questions related to the second generation of the Koa web framework for Node.js, whose middleware function signature changes from use of generators to async/await.

should be used for questions related to the second generation of the Koa framework. Note also the tag for questions about the previous version.

Sample

Koa2 changes the middleware function signature:

// Middleware functions use async where required. Must return a promise.
app.use(async (ctx, next) => {
  try {
    await next() // next is now a function
  } catch (err) {
    ctx.body = { message: err.message }
    ctx.status = err.status || 500
  }
})

app.use(async ctx => {
  const user = await User.getById(this.session.userid) // await instead of yield
  ctx.body = user // ctx instead of this
})

References

235 questions
4
votes
0 answers

sequelize Bulkcreate: Ignore if single row fails

I have to import rows from a .csv, and put them in my database (chunks of 500). To do this i'm using the sequelize bulkCreate functionality. I have a field called address which is required (not nullable). If one of the rows fails to insert (because…
Nicolas
  • 4,526
  • 17
  • 50
  • 87
4
votes
0 answers

Knex leaves open server when using Jest (recommendation)

I'm trying to do some TDD with my following stack Jest Node Koa2 SuperTest Knex Objection My problem starts with the open handler of the koa server and I could solve that with the instance of the server and close it with server.close() However, I…
Archagy
  • 135
  • 1
  • 12
4
votes
2 answers

Error handling in async await

How can you implement error handling for Mongoose (current version v5.1.5)? For example, let's assume the following code where a user detail is being looked up. let u = await user.find({ code: id }).lean(); return u; And some error occurs, how…
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
4
votes
0 answers

Setup React using CAS for authentication

I am trying to make a react-app that uses a CAS(Central Authentication Service) for authentication for certain URLs. I am using express to run the server by using a CAS client library koa-cas2 https://www.npmjs.com/package/koa-cas2 Here's my…
Shivam Kumar
  • 121
  • 2
  • 8
4
votes
1 answer

Koa2: how to write chain of middleware?

So in express, we can have a chain of middleware, copies an example: middleware = function(req, res){ res.send('GET request to homepage'); }); app.get('/', middleware, function (req, res) { res.send('GET request to homepage'); }); What's the…
user3552178
  • 2,719
  • 8
  • 40
  • 67
4
votes
1 answer

Koa is being executed twice per request?

Is there any reason why Koa is being executed twice per request? const Koa = require('koa') const app = new Koa() const index = async(ctx, next) => { console.log('Hello world!') await next() ctx.body = 'Hello…
Run
  • 54,938
  • 169
  • 450
  • 748
3
votes
0 answers

koa ctx.redirect not working after fetch request

I am using ctx.redirect from koa not able to understand why it is not working It is not working in this case if (shopify && shopify.accessToken) { const response = await fetch( `https://${shopify.shop}/admin/metafields.json`, …
Ravi kumar
  • 61
  • 5
3
votes
0 answers

Nodejs How to handle multiple request that may generate conflicts between them?

So, I'm building an Ecommerce Api with Koajs and Koa router, and i'm now handling orders payment request, the thing is, before the actual paying, the status of my order model change to 'Waiting for payment', and it is at this point when my API…
3
votes
3 answers

How to upload file using Koa?

I am setting up a new server and want to support advanced upload function. Firstly I need to validate file (filetype, filesize, maxcount), and finally upload it to some destination. I tried something with koa-multer but I cannot get multer…
Benfactor
  • 543
  • 4
  • 11
  • 31
3
votes
2 answers

Typeorm connect to multiple database

I use node.js , TS and typeorm for back-end project. I need to connect to a different database in the middleware according to the parameter I send. And I've got to send the query to the database. ormconfig [ { "name": "default", "type":…
anıl yıldırım
  • 953
  • 1
  • 10
  • 17
3
votes
1 answer

Require returns an empty object when using jest and supertest to test koa2

I am learning test, for now I using jest and supertest to test koa2 server, and I test features is login, but I always receive false when test login feature, I find the require returns an empty object in my controllers/users.js after long time…
Roy
  • 731
  • 3
  • 10
  • 24
3
votes
2 answers

Node Koa cors is not working

use koa + react server.js guide from npm koa const Koa = require("koa"); const KoaRouter = require("koa-router"); const static = require("koa-static"); const cors = require('koa2-cors'); const fs = require("fs"); const path = require("path"); const…
Jarvis
  • 371
  • 1
  • 10
  • 22
3
votes
1 answer

Koa-router ignores async/await of Mongoose and return 404 always

This is the code of route. When I use commented Promise, it returns 123 in body. But with mongoose query it returns 404 status. Item in log founds good. But it seems router just ignore await and return 404 immediately. What am I doing…
Disorder
  • 167
  • 2
  • 13
3
votes
1 answer

Koa-router getting parsed params before hitting route

I'm using koa2 and koa-router together with sequelize on top. I want to be able to control user access based on their roles in the database, and it's been working somewhat so far. I made my own RBAC implementation, but I'm having some trouble. I…
Nict
  • 3,066
  • 5
  • 26
  • 41
3
votes
1 answer

Image file download or show in browser

I am making an image upload app with Node, Koa2, Mongo, GridFS, and React. Currently I have no problem uploading files with this code: // HANDLE POST MULTIPART FORM DATA app.use(async function(ctx, next) { if ( !ctx.request.is('urlencoded',…
mravich
  • 33
  • 7
1
2
3
15 16