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

Best way to add helper methods to context object in Koa2

I would like to add method such as view and json to the context object passed to my controllers. I do this in a middleware that runs before everything else: async function(ctx, next){ ctx.view = view.bind(ctx); ctx.json = json.bind(ctx); …
Megakoresh
  • 746
  • 1
  • 11
  • 30
3
votes
1 answer

Nodejs, store bin files as BYTEA into pgsql (corrupted files)

For some reason I need to store some files (mostly images or pdfs) into my database (PG 9.2.20). Those files are uploaded by users and when I download them back, they are corrupted. I'm working with nodejs. The column type I store the file is…
thesearentthedroids
  • 588
  • 1
  • 7
  • 24
3
votes
0 answers

koa's redirect function doesn't work well

when I use ctx.redirect, koa just sends GET request but not actually redirect the tab to other page from where It was. I'm just trying to redirect to user's page after signup. Here's code. signup router.post('/signup', async (ctx, next) => { let…
Phillip YS
  • 784
  • 3
  • 10
  • 33
3
votes
0 answers

why my koa-passport authenticate parameter user is always undefined?

I'm trying to use koa-passport for koa2, and followed the examples of the author, but i always get "Unauthorized". I used the console.log and found that it even not hit the serializeUser. var UserLogin = async (ctx, next) =>{ return…
Vega
  • 33
  • 1
  • 6
3
votes
5 answers

Koa send status 404 every time is

export async function getPlaces(ctx, next) { const { error, data } = await PlaceModel.getPlaces(ctx.query); console.log(error, data); if (error) { return ctx.throw(422, error); } ctx.body = data; } Koa everytime sends…
3
votes
1 answer

External Modules in Typescript when targeting ES6

With the release of Typescript 1.7 and async/await support, I thought it would be a good time to try out Typescript with koa@2. I have a really simple setup and it kind of works already: // app.ts ///
Andreas Gassmann
  • 6,334
  • 7
  • 32
  • 45
3
votes
2 answers

How to mock external service when testing a NodeJS API

I have JSON API built with koa which I am trying to cover with integration tests. A simple test would look like this: describe("GET: /users", function() { it ("should respond", function (done) { request(server) …
silkAdmin
  • 4,640
  • 10
  • 52
  • 83
2
votes
1 answer

How to link routes with controllers in node js?

I have some API's written in server.js. I want to make MVC structure like i want to have all routes in routes directory and APIs in controller. How i can make this structure with koa and nodejs. I'm new in nodejs. I try to do something but its not…
user12561026
2
votes
1 answer

How to NOT respond at all to a request using koa 2?

I want to implement a shadowban. Above a certain rate limit, I want to not respond anything to a request. I couldn't find how to do that in the doc. It seems setting body = null will still trigger a response. How may I prevent koa@2 from answering…
Offirmo
  • 18,962
  • 12
  • 76
  • 97
2
votes
1 answer

What happens when you don't wait for for a promise's resolution in a koa middleware?

I'm working on improving the response time for one of our API routes. We make a couple of requests to post messages to a message queue, and currently we await the network request. const someMiddlewareFunction = async (ctx, next) => { ... …
UnbrandedTech
  • 461
  • 6
  • 14
2
votes
1 answer

How to create Koa2 middleware which will modify response body and run last in a chain?

I have a Koa2 application, which renders templates on different routes. I'd like to introduce a middleware which will modify rendered templates in some way and I need it to be the last in a chain of other middlewares. Are there any way to enforce…
i9or
  • 1,998
  • 1
  • 14
  • 20
2
votes
1 answer

How to pass a parameter in Koa middleware with context?

I am using nodejs koa rest api service. And I want to pass a parameter to validation middleware. But I also need to pass context. How can I correctly use middlewares with koa2 //route.js const Router = require('koa-router') const auth =…
Benfactor
  • 543
  • 4
  • 11
  • 31
2
votes
2 answers

How to pass a parameter in Koa middleware?

So I have this function in Koa, that basically checks if a user can access a specific route. exports.requireRole = async role => async (ctx, next) => { const { user } = ctx.state.user; try { const foundUser = await…
truncate789
  • 31
  • 1
  • 2
2
votes
2 answers

Koa SSE connection reconnecting

I have set up an SSE connection using Koa like so: const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const router = new Router(); // Sets up the HTTP header and sends a message via SSE function writeSSE(ctx,…
Christopher
  • 1,712
  • 2
  • 21
  • 50
2
votes
0 answers

In Koa.js how to upload a CSV file and read its content and import it into MongoDB collection?

In Koa.js how can we upload a CSV file and read the uploaded file to import it into MongoDB collection? What is the efficient way to do this?
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
1 2
3
15 16