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

Exporting routes in Koa

I'm having a strange issue when exporting my routes. For some reason, this code works for me: app.js import Koa from 'koa' import routes from './routes/index' const app = new Koa() app.use(routes) app.listen(3000, () => { console.log('Server…
Saad
  • 49,729
  • 21
  • 73
  • 112
0
votes
1 answer

Getting 204 using koa2 and koa-router for REST api - response body not being passed

I'm coming from Express and trying to learn Koa2 for a new project that I'm working on, but I'm struggling getting the most basic Get operation working for my app. On the server side I have a route setup that is hitting an authorization server…
Zigrivers
  • 409
  • 7
  • 18
0
votes
1 answer

mongoose findbyId internal server error

I have a collection of users: db.users.find() { "_id" : ObjectId("56d9f3435ce78127510332ea"), "index" : 1, "isActive" : false, "name" : "Noble", "surname" : "Downs", "email" : "nobledowns@conferia.com", "phone" : "+1 (812) 412-3775", "address" :…
user2670996
  • 2,654
  • 6
  • 29
  • 45
0
votes
1 answer

mongoose findbyId Internal error

I'm using Koa 2 and koa-router. The router file: import User from '../models/user' var router = require('koa-router')(); router .get('/', async ctx => ctx.body = await User.find({})) .get('/:id', async (ctx, next) => ctx.body = await…
user2670996
  • 2,654
  • 6
  • 29
  • 45
0
votes
1 answer

How to pass value to Koa2

I have an react app that I want to pass value down to a koa server. let data = new FormData() data.append('json', JSON.stringify(token)) fetch('/charge', { method: 'POST', body: data }) .then((res) => { return res.json() }) …
Well
  • 191
  • 3
  • 8
-1
votes
1 answer

Webpack with node backend on single process/same port with hot reload for both front and back…?

I have a pretty standard setup with a node backend that serves an SPA as a webpack bundle as well as the api serving that SPA application. The backend is using koa2. So I have had hot reload working just fine for the front end part but I now have a…
Viktor Hedefalk
  • 3,572
  • 3
  • 33
  • 48
-1
votes
1 answer

TypeError: Cannot read property 'eslint' of undefined

I have this problem when I used nuxt-community/koa-template in github, I have already used such instructions as "NPM install",but problem still remain. Please give me some solutions.
cyzs123456
  • 1
  • 1
  • 2
-1
votes
1 answer

JWT is not stored... How to view it?

Can't understand the nodejs Authentication (KoaJS2). I have this piece of code : router.post("/login", koaBody, (ctx, next) => { const data = ctx.request.body; db .any("SELECT * FROM users ORDER BY id ASC") .then(function(data) { …
-1
votes
1 answer

React-router and server sided rendering

I would like to render server sided with react-router and Koa. However I only got it to work if I included the full node build. This is duo to my components requiring sass compiling, image handling and the like. It would be great if my front end…
CWdesigns
  • 27
  • 6
-1
votes
1 answer

How to read ctx.request.body in koa 2 (without body-parser)?

I want to read ctx.request.body from a post without body-parser middleware. const Koa = require('koa'); const Router = require('koa-router'); const app = new Koa(); const router = new Router(); app.use(router.routes()); router.post('/publish',…
1 2 3
15
16