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

How can I force a browser navigation based GET from a React Route?

I have a login page in my React App that provides the user with the choice of a local login or login via social network OAuth2 services utilising passport.js on a Koa based API server. Initially, I was attempting to call the backend via ajax, but it…
Dallas
  • 888
  • 3
  • 10
  • 24
0
votes
1 answer

Execute a middleware one-time only at server startup in Koa v2

I created this middleware which executing only once when any route in the website gets the first hit from a visitor: // pg-promise const db = require('./db/pgp').db; const pgp = require('./db/pgp').pgp; app.use(async (ctx, next) => { try { …
Lanti
  • 2,299
  • 2
  • 36
  • 69
0
votes
2 answers

Serve static files with Koa2

How do I serve static files with Koa2 when someone visitis route? I tried milions of things and I always get not found message. Server has responded with status 404 import 'babel-polyfill' import co from 'co' import path from 'path' import render…
Igor-Vuk
  • 3,551
  • 7
  • 30
  • 65
0
votes
1 answer

How to get response body from a http.request as a ctx.body for koa?

I'm learning Node.js, and for some reason I need to set up a http.request to another ip, and get the response from it and apply to ctx.body of koa. I wrote the code like this: var refreshData = async (ctx, next) =>{ var option = { host:…
0
votes
2 answers

Koa2 Same Route Different Params

I have two endpoints: GET all users by ID http.get(API_URL + "users/" + id) GET all users by username http.get(API_URL + "users/" + username) Can I send different parameters on a same route: router.get("/users/:id", async ctx => { …
Klark
  • 423
  • 6
  • 16
0
votes
2 answers

When would this Koajs error happen

I'm trying to get my head around Koa and I am making small progress. At the moment I think I understand this code import Koa from 'koa'; import router from './router'; const app = new Koa(); app.use(async (ctx, next) => { try { await…
relidon
  • 2,142
  • 4
  • 21
  • 37
0
votes
1 answer

this undefined: possible async to generator issue

I'm getting 'client' undefined from a library I am using for firebase rest transactions, and it is one I am pretty sure I used before with no problems. When I look in the library (the intellij websphere debugger takes me to a typescript file, must…
roberto tomás
  • 4,435
  • 5
  • 42
  • 71
0
votes
1 answer

Koa middleware type definitions requires a Middleware member export

I'm using koa with Typescript. I also use the koa middleware koa-static and koa-bodyparser. I have the type definition packages @types/koa, @types/koa-bodyparser and @types/koa-static installed. When I run tsc I get the following errors: ERROR in…
gwest7
  • 1,556
  • 19
  • 26
0
votes
1 answer

koa.js returning 404 "Not Found" after signup, instead of the token from ctx.body

I'm failing to return my token to actually be retrieved from my middlewares using xhr agents say supertest and postman. after I signup my user. I'm trying to get the jwt from body so i can make authenticated requests, but for some reason i keep on…
ArchNoob
  • 3,946
  • 5
  • 32
  • 59
0
votes
2 answers

koa-static going to next middleware

I have a koa 2 server. The following code are my middlewares: // parse body app.use( bodyParser() ) // serve static app.use( serve( path.join(__dirname, '/public') ) ) // routes app.use( routes ) // error middleware app.use( async ctx =>…
Anderson
  • 331
  • 1
  • 4
  • 18
0
votes
1 answer

NODE JS - TypeError: Path must be a string. Received { request

Hello guys (and girls) ^^ I'm using koa2, koa-router, koa-static (try..) and no koa-send... but nothing work and i need your help. :D So in fact, my js files are not find... and i have this ... So i have installed koa-static and did this:…
Ravaniss
  • 21
  • 1
  • 1
  • 8
0
votes
0 answers

koa2 in multer TypeError: fn.apply is not a function

I am studying koa2 using Multer to post images to a server. mine usage like the document it show TypeError: fn.apply is not a function at Object.
sun
  • 1
0
votes
1 answer

Asynchronous functions in constructor

I'm trying to export a class with an asynchronous call in the constructor: my.js: module.exports = class My extends Emitter { constructor () { super() this.db = Object.create(db) this.db.init(cfg) } } db.js: module.exports = { …
Patrick
  • 7,903
  • 11
  • 52
  • 87
0
votes
2 answers

passport.authenticate is not working at all

I am trying to make social login in KOA2 using passport. When I try to authenticate the user with the help of passport.authenticate(). It is supposed to be executed and should throw me to the fb login page, but is is not doing so. Even it is showing…
Surya Purohit
  • 1,090
  • 1
  • 9
  • 29
0
votes
1 answer

How to handle invalid GET request in Koa 2?

Below is the simple Koa server that I have setup. However, every time when an invalid GET request is performed, the server "hangs", as in the network resource tab in Chrome would specify pending. server.js const app = new Koa(); const apiUrl =…
cusX
  • 470
  • 1
  • 6
  • 17
1 2 3
15
16