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

Using koa-jwt with koa-router

I am implementing the a Nextjs service with koa, koa-router and kow-jwt, but I'm confused with the routing setting with them. My project have 2 pages, one is dashboard and the other is login. The dashboard need to pass the verification and the login…
Jimmy Lin
  • 1,481
  • 6
  • 27
  • 44
2
votes
1 answer

Is it required to use async functions in koa framework?

I started learn koa.js and on koa documentation I found such demo code: const Koa = require('koa'); const app = new Koa(); app.use(async ctx => { ctx.body = 'Hello World'; }); app.listen(3000); This middleware use async function, but code is…
2
votes
1 answer

Koa JS - Cannot Send Response after calling an API. Always Getting 404

Here is my code : const koa = require('koa'); const app = new koa(); const https = require('https') let email = "myemail@gmail.com" let password = "mysecret" var options = { host: 'myhost', port: 443, path: '/api/login?email=' + email…
Apal Shah
  • 680
  • 8
  • 17
2
votes
3 answers

Access POST data in node using koa-body and koa-router

Making a ajax POST from a User Agent $.ajax({ type: 'POST', url: 'https://mysub.domain.dev/myroute', headers: { 'X-Requested-With': 'XMLHttpRequest' }, contentType:…
Kickaha
  • 3,680
  • 6
  • 38
  • 57
2
votes
1 answer

Why ctx.state did not pass to another middleware?

use koa2 ejs koa-router, ejs template how to use another middleware's ctx.state localhost:3000/admin/usermsg admin.get('/usermsg', async(ctx) => { ctx.state.userMsg = { page: Number(ctx.query.page), limit: 4, …
zrd
  • 21
  • 1
  • 2
2
votes
1 answer

How to apply middleware to all routes?

How I want to apply middleware to all paths in koa-route, e.g. router1.use( (ctx) => { console.error("hello 0 ..."); console.log(ctx.url); } router1.all( (ctx) => { console.error("hello 0 ..."); console.log(ctx.url); } It comes back…
user3552178
  • 2,719
  • 8
  • 40
  • 67
2
votes
0 answers

Using passport & passport.authenticate AFTER route declaration

The application I am working on requires that all authentication code be placed after route declaration i.e. instead of separating routes into public and private routes by a middleware and calling passport.authenticate inside the callback for the…
amp
  • 109
  • 1
  • 1
  • 9
2
votes
3 answers

Is there any way to rename the upload image with koa?

I've only used koa-bodyparser and I just found out that It doesn't parser form-data that allows to upload files. so I'm trying these modules co-busboy, koa-body, koa-better-body. but I couldn't figure out how to rename the upload file before save…
Phillip YS
  • 784
  • 3
  • 10
  • 33
2
votes
1 answer

ctx.passport undefined when trying to generate a jsonwebtoken when using passport with koa?

I'm trying to create a simple user register/login api using Koa 2 and passport. The trouble comes when trying to login. Here is the code for the route; import { authEmail, generateToken } from '../../auth'; import User from…
alexc
  • 1,250
  • 2
  • 17
  • 44
2
votes
1 answer

Getting entity's id inside a transaction

I have a datastore transaction where I create an entity (a user) letting datastore generate the ID for me. I then would like to use that ID so that I can create another entity (of another kind). While this is possible with regular datastore 'save'…
2
votes
3 answers

How to write a async middleware in KOA 2

I want to resolve a promise and then render a view like so in Koa 2. async function render(ctx, next) { // wait for some async action to finish await new Promise((resolve) => { setTimeout(resolve, 5000) }) // then, send response …
Shubham Kanodia
  • 6,036
  • 3
  • 32
  • 46
2
votes
2 answers

Router is not defined in KOA2

I have two files, one of them is the app.js and the otherone is api.js. In the first file I have : app.use(setHeader) app.use(api.routes()) app.use(api.allowedMethods()) And in api.js I have: import KoaRouter from 'koa-router'; const api =…
nole
  • 1,422
  • 4
  • 20
  • 32
2
votes
0 answers

Koa2 + Mongoose + async/await

Trying a basic thing in Koa2, fetching results from MongoDB and sending it in response. Following is my code with koa-router. If I try to send the record in ctx.body, the result is always "Not Found" response. Help!! import Router from…
Zaje
  • 2,281
  • 5
  • 27
  • 39
2
votes
2 answers

How to run simple app with koa2?

Problem I am trying to run simple http server using koa2, but have problems running it. It uses es6 that is expected to work in future node.js versions and I was wondering how can I run it with node v6.1.0 ? Code import Koa from 'koa'; const app…
gevorg
  • 4,835
  • 4
  • 35
  • 52
2
votes
3 answers

Webpack-hot-middleware with Koa 2.0

Is there a way to integrate webpack-hot-middleware and webpack-dev-server with Koa? I am having hard time find working example which is not up to date fork of webpack-hot-middleware code.
Adam Močkoř
  • 21
  • 1
  • 2