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
2
votes
1 answer

Display custom page when error happens in Koa

Here is the background of the question : I'm following the kick-off-koa using Koa 2. But the exercises in the kick-off are designed for Koa 1. I've created an issue for this problem of Koa 2 : Task of error handler with Koa 2 cannot pass. For short,…
Qianyue
  • 1,767
  • 19
  • 24
1
vote
1 answer

GET uncertain amount of request parameters with koa router

if a define a route like this: router.get('/:name/:age', async (ctx, next) => { ctx.body = ctx.params }) when i access the url: http://localhost:3000/vortesnail/18, I'll get: { name: 'vortesnail', age: '18' } Here is my problem: if i access…
vortesnail
  • 31
  • 5
1
vote
1 answer

Why is Koa's redirect throwing 404 after another redirect?

I have the following code... const auth = new Authentication(); mainApp.use(auth.checkToken.bind(auth)); mainApp.use(mount('/auth', auth.app)); class Authentication{ constructor() { this.redirectApp = new Koa(); …
Jackie
  • 21,969
  • 32
  • 147
  • 289
1
vote
0 answers

koajs: how to wait for client to receive file?

I want to return a generated file with koa (2) and after the client finished receiving the file, the file should be deleted. // this function is called by Koa async myMethod (ctx) { const file = await something()// generate a file …
zingi
  • 1,141
  • 13
  • 23
1
vote
1 answer

How to secure Koa.js application against CSRF attacks?

I know that Cross-Site Request Forgery (CSRF) is an attack that forces an user to execute unintentional actions some web application in which they are already logged in. I want to prevent CSRF on calls being made to my Koa.js based APIs and form…
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
1
vote
1 answer

pm2 + KOA process.send is undefined

I am trying to accomplish the "graceful start" with pm2, but somehow my process.send is always undefined. I am using esm module and start my application with yarn start I am logging the process.send, but somehow it is always…
bukso
  • 1,108
  • 12
  • 23
1
vote
1 answer

Open handles while testing Koa.js with Jest

Here's a trivial trivial koa server implementation: const Koa = require('koa') const api = require('./resources') const createServer = () => { const app = new Koa() app.use(api.routes()) app.use(api.allowedMethods()) return…
boojum
  • 669
  • 2
  • 9
  • 31
1
vote
2 answers

How to extract information from a JWT in node js without any module?

hellow, could you helpme please? I am trying to create a function to protect a POST and DELETE method. I have the problem that all authenticated users of my application have the ability to delete the posts of other users if they know the id of that…
geraktOfRivia
  • 325
  • 3
  • 7
  • 19
1
vote
0 answers

Why is my koa application timing out when testing a route with Jest/Supertest

Summary of Problem Receiving : Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Timeout when trying to run a test with Jest and supertest. Specs Koa2 project, Jest/Supertest testing, Babel 7.9.0…
Michael Jay
  • 503
  • 3
  • 15
1
vote
1 answer

Fetch image and send it using koa2

I am trying to fetch an image from external URL and then send it as a response in koa2. For fetching the image I am using Axios library. I am trying to do that the following way: router.get('/get-image', async (ctx, next) => { const {authToken}…
David Novák
  • 1,455
  • 2
  • 18
  • 30
1
vote
1 answer

Using koa-views with TypeScript

I was trying to use koa-views as my renderer, but I kept getting the message from TS: Property 'render' does not exist on type 'ParameterizedContext>'.ts(2339) My app.js import Koa from "Koa"; import views from…
Kiyoshi
  • 13
  • 5
1
vote
0 answers

How do I load Koa context into my Winston logger middleware?

I have a koa server and my frontend has been passing in some user-related data via koa's context. In my koa app, I can get the context by doing the following: // app.ts import { Context } from "koa"; this.app.use(async (ctx: Context, next: () =>…
James Li
  • 133
  • 10
1
vote
2 answers

How can I test koa middleware with typescript

I have a lot of middleware. Here is one of them. How can I test my middleware with type compliance and with context.state validation on typescript ? async function internationalizationPlugin( context: ParameterizedContext, …
polRk
  • 207
  • 4
  • 16
1
vote
1 answer

Convert raw http response to Koa compatible object?

I have a basic HTTP server created with the Node's http module: var http = require('http'); http.createServer(function (request, response) { response.end(); }).listen(8080); I'm getting the request and response objects from this raw http server,…
Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160
1
vote
1 answer

Is there analog of express's app.set( 'something', something ) in koa?

I need socket.io instance in several places in my app. To achieve this in express i can do this: app.set('io', io); In koa right now i have this: app.use( async ( ctx, next ) => { ctx.io = io; await next(); }); This works, but…
Mnemonic Pie
  • 29
  • 1
  • 5