Questions tagged [koa-router]

Router middleware for koa.

Router middleware for koa.

https://github.com/alexmingoia/koa-router

139 questions
4
votes
1 answer

Koa cookie returning `undefined`

After a POST request is sent from the browser to the /generate url in the server, I want to create a string and save it as a cookie. When a GET request is later sent from the browser to the /retrieve url in the server, I want to send that string as…
Saad
  • 49,729
  • 21
  • 73
  • 112
3
votes
3 answers

Koa - Catch all routes that start with prefix

I was wondering how can we catch all API call of a specific route with KOA-Router. For example. I have an api designed like this : /api/ /api/user/ /api/user/create /api/user/login /api/user/delete How can I trigger the route /api/ for all calls…
Tryall
  • 640
  • 12
  • 22
3
votes
1 answer

Koa-router ignores async/await of Mongoose and return 404 always

This is the code of route. When I use commented Promise, it returns 123 in body. But with mongoose query it returns 404 status. Item in log founds good. But it seems router just ignore await and return 404 immediately. What am I doing…
Disorder
  • 167
  • 2
  • 13
3
votes
1 answer

Koa-router getting parsed params before hitting route

I'm using koa2 and koa-router together with sequelize on top. I want to be able to control user access based on their roles in the database, and it's been working somewhat so far. I made my own RBAC implementation, but I'm having some trouble. I…
Nict
  • 3,066
  • 5
  • 26
  • 41
3
votes
0 answers

405 Method not allowed in koa-router

I am getting 405 Method Not Found as response when using PUT and DELETE request using POSTMAN. My app.js file looks like this const Koa = require('koa'); const serve = require('koa-static'); const cors = require('@koa/cors'); const userRouter =…
3
votes
1 answer

Define koa-router nested routes w/ prefixes

I'm trying to define different routes using koa-router and I'm having a hellova time getting it working. Something like this: const apiRouter = new KoaRouter({ prefix: '/api' }) .use(bodyParser) .post('/sign-in',…
RavenHursT
  • 2,336
  • 1
  • 25
  • 46
3
votes
0 answers

Koa middleware ordering

I'm having a problem with koa js and middleware order. Im' using multiple middlewares, Koa-router, formidable, koa-static-folder and one for setting headers. Right now, with current order, when I'm uploading a file from the frontend to backend via…
Michal Takáč
  • 1,005
  • 3
  • 17
  • 37
3
votes
3 answers

How to combine separated Koa-Routers with Typescript

I decided to split my routers by their purpose, so they look like this: routers/homeRouter.ts import * as Router from 'koa-router'; const router: Router = new Router(); router .get('/', async (ctx, next) => { ctx.body = 'hello world'; …
Fuchur Kool
  • 53
  • 2
  • 7
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
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
2 answers

How to skip or jump middleware? (node)

Given two middleware, how can you add something that would get hit first, then determine which of the middleware to pass the request onto. The request can only be dropped or forwarded to one of the middleware, never both. app.use( function *(next)…
House3272
  • 1,007
  • 5
  • 14
  • 29
3
votes
1 answer

How do you run `yield next` inside a promise or callback?

I'm stuck writing an authentication router for a koa app. I have a module that gets data from the DB then compares it to the request. I want to only run yield next if the authentication passes. The problem is that the module that communicates with…
hal
  • 4,845
  • 7
  • 34
  • 57
2
votes
1 answer

How to optimize router in nodejs koa?

I use koa to create a nodejs application, and I try to optimize. app.js 'use strict'; const Koa = require("koa"); const app = new Koa(); const Router = require("koa-router"); const router = new Router(); const stuff =…
Bill
  • 101
  • 8
2
votes
0 answers

In Koa, can I access Koa-router context at middleware invocation?

I want to pass a parameter into middleware router.get( '/:id', myMiddlware({projectid : ctx.request.query.projectid } ), async (ctx) => {...} ) I need to get the value of 'projectid' in the middleware declaration but of course, ctx from…
Rob
  • 1,297
  • 2
  • 16
  • 30
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
1
2
3
9 10