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
18
votes
2 answers

Why do we await next when using koa routers?

Why do we do this router.get('/data', async (ctx, next) => { ctx.body = dummyjson.parse(data); await next(); }); router.get('/data/:x', async (ctx, next) => { const newData = dataRepeat.replace('%(x)', ctx.params.x); ctx.body =…
relidon
  • 2,142
  • 4
  • 21
  • 37
13
votes
3 answers

Adding properties to Koa2's context in Typescript

I want to use Facebook's DataLoader with Koa2 in Typescript. I want per-request DataLoader instances to go with my per-request database connections. How is this best achieved? My current approach is to augment the Koa2 context but I'm failing…
sdc395
  • 255
  • 1
  • 3
  • 12
12
votes
1 answer

Koa2 - How to write to response stream?

Using Koa2 and I'm not sure how to write data to the response stream, so in Express it would be something like: res.write('some string'); I understand that I can assign a stream to ctx.body but I'm not familiar with node.js streams too well so…
user3690467
  • 3,049
  • 6
  • 27
  • 54
11
votes
1 answer

Why need more than one secret key on koa?

Sorry, I don't quite understand how secret key works in koa. In koa, there is a keys field on app object which will be used like this: const app = new Koa(); app.keys = ['some secret', 'another secret', 'or more ...']; // it's an …
Mas Bagol
  • 4,377
  • 10
  • 44
  • 72
10
votes
1 answer

How to redirect some URL conditionally in the koa 2

This is what i'm thinking, pseudo code. const myRedirect = (routePath) => { newUrl = routePath; if (matches condition) newUrl = do_some_modification(routePath); return next(newUrl); } const myFunc = (routePath,…
user3552178
  • 2,719
  • 8
  • 40
  • 67
9
votes
3 answers

How to get koa-router query params?

I haved used axios.delete() to excute delete in frontend ,code like below Axios({ method: 'DELETE', url:'http://localhost:3001/delete', params: { id:id, category:category } }) And I used koa-router to parse my…
frankkai
  • 145
  • 1
  • 2
  • 9
8
votes
1 answer

Koa.js and streaming. How do you handle errors?

Does anybody works with koa.js and streams? Consider this example const fs = require('fs'); const Koa = require('koa'); const app = new Koa(); app.use(async (ctx) => { ctx.body =…
Stepan Kuzmin
  • 1,031
  • 2
  • 11
  • 21
7
votes
3 answers

Nodejs - how group and export multiple functions in a separate file?

How can I group and export multiple functions in nodejs? I am trying to group all my util functions in utils.js: async function example1 () { return 'example 1' } async function example2 () { return 'example 2' } module.exports = {…
Run
  • 54,938
  • 169
  • 450
  • 748
7
votes
2 answers

REST API with koa2. Common prefix for several routers

I have two entities, users and employees. So I want CRUD for both in different endpoints, but both of them will be mounted under "api", so I can define api_v1, api_v2 and so on. The endpoints would be something like: get api/users put…
user2670996
  • 2,654
  • 6
  • 29
  • 45
6
votes
1 answer

How to send a http response using koajs

I'm trying to validate a webhook via facebook. So facebook hits my url my-url/facebook/receive within my route in nodejs i'd do res.send(req.query['hub.challenge']); to send an http response. I'm using KoaJS. From what i understand, Koajs merges the…
pourmesomecode
  • 4,108
  • 10
  • 46
  • 87
6
votes
2 answers

Mongoose async/await with Koa get stuck on await Model.findOne().exec()

I have an Koa 2 application, and a post to /signup is handled by this function: import User from 'models/user'; export const signup = async (ctx, next) => { const { email, password } = ctx.request.body; try { const existingUser = await…
Lucas Katayama
  • 4,445
  • 27
  • 34
5
votes
1 answer

Unable to send authenticated request in tests using Jest, Supertest, Passport, Koa2 Again

I've been trying to use internetross's March 2018 answer to no avail. I too am using Jest, Supertest, and in my case Koa and Passport. Using REST client in Visual Studio, no problem. The session gets pass through, Passport authenticates, and I get…
Crivens
  • 333
  • 3
  • 13
5
votes
1 answer

Koa2 request.body is empty

--- This question is pretty old and libraries used here are most likely outdated, the solution is still correct, but please use up-to-date versons --- I am working on a web service with koa2 and node6. My koa dependencies as follows; "koa":…
Uğurcan Şengit
  • 976
  • 1
  • 11
  • 31
5
votes
1 answer

Koa-session getting reset after appending object to it?

I have a controller which looks up a character, and then does some stuff with it, the controller looks like: router.post('/profile/characters', async ctx => { try { ctx.type = 'json'; let req = ctx.request; if…
Datsik
  • 14,453
  • 14
  • 80
  • 121
4
votes
2 answers

Combining Typescript Koa-Router and Passport

I am newb in Typescript and trying to integrate koa-router and koa-passport. Installed all @types\ import Koa from "koa"; import Route from "koa-router"; import passport from "koa-passport"; import session from "koa-session"; const app = new…
Talgat Saribayev
  • 1,898
  • 11
  • 19
1
2 3
15 16