Rahul Sharma
Rahul Sharma

Reputation: 10111

how to get the path of a route inside middleware in Koa

How to get the actual path of the route without params.

Getting /users/abc expecting /users/:id

Route

router.get('/user/:id', find);

Middleware

const middleware= (ctx, next) => {
    let path = ctx.path; // '/users/abc'
   // Something 
};

Upvotes: 1

Views: 1245

Answers (1)

lx1412
lx1412

Reputation: 1200

const middleware= (ctx, next) => {
    let path = ctx._matchedRoute; // '/users/:id'
   // Something 
};

Source code

https://github.com/alexmingoia/koa-router/blob/master/lib/router.js#L336

https://github.com/alexmingoia/koa-router/blob/master/lib/layer.js#L46

Upvotes: 3

Related Questions