question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question: How to get route.param on a RoutingController?

See original GitHub issue

I was trying to…

I have a very specific scenario giving the following situation where I need to check on all my routes if a param resourceType exists or not add some params to the request object giving my param value:

app.param('resourceType', (req, res, next, resourceType) => {
  try {
     const { 
        getResource,
        getResources,
        getUsers,
        getRoles,
        updateRole,
        deleteRole,
        createSuperAdmin,
        deleteSuperAdmin,
      } = getResourceRoutersParams(resourceType);

    req.resource = {
        getResource,
        getResources,
        getUsers,
        getRoles,
        updateRole,
        deleteRole,
        createSuperAdmin,
        deleteSuperAdmin,
    };

     next();
    } catch (e) {
      logger.error(e);
      throw new ResourceNotFoundError();
    }
})

I’m solving the issue using the app.param API

The problem:

All my routes contain a :resourceType param:

  • GET /resources/:resourceType/:resourceId/users
  • GET /resources/:resourceType/roles
  • GET/PUT/DELETE /users/:userId/:resourceType
  • GET/PUT/DELETE /users/:userId/:resourceType/:resourceId

There is any way to implement it other than adding a middleware for each route?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
golden-protocolcommented, Apr 22, 2021

Ah ok. That makes sense. Sorry for the wrong answer 🤦🏾 If you find a more elegant solution I would be curious as to what you do.

0reactions
github-actions[bot]commented, Apr 3, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass routing parameter to controller constructor
Why does it have to actually be passed into the constructor? As a route value, you can already access it any time via:...
Read more >
Accessing named parameter in custom route from within a ...
This works fine but how can I access that <order> named parameter within my controller? Is the only way to use craft::$app->request->getSegment( ...
Read more >
Everything You Should Know About ASP .NET Core Routing
There are some route parameters which are identified by framework, e.g. controller , or action are most common templates that you can find...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
Route parameters are named URL segments used to capture values at specific positions in the URL. The named segments are prefixed with a...
Read more >
Dynamic Routing with Route Parameters - Adocasts
Here, we've named our route parameter id to signify that the route parameter is meant to be a post id value. So regardless...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found