Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use colon as normal character instead of route param #3857

Closed
isayme opened this issue Jan 16, 2019 · 7 comments
Closed

How to use colon as normal character instead of route param #3857

isayme opened this issue Jan 16, 2019 · 7 comments

Comments

@isayme
Copy link

isayme commented Jan 16, 2019

Colon(:) used to define route param in Express/Koa.

app.get('/users/:userId/books/:bookId', function (req, res) {
  res.send(req.params)
})

Google API design use colon as custom verb
image

Question: Is there a way to define a route in which I want use colon as normal character ?

Target: define a route that match PUT /tasks/1234:undelete, and define with string instead of regexp.

@isayme
Copy link
Author

isayme commented Jan 17, 2019

If you use Koa koa-router, try router.put('/tasks/:_id\\:undelete').
If you use Express, maybe not for now.

Reason: path-to-regexp>=0.2 support \\:, but express use 0.1.7 currently.

Related issues: #3419 #3409 #3142 #2530

@isayme isayme closed this as completed Jan 17, 2019
@zackfalcon
Copy link

Hello is this not working yet or maybe express have done an update to done this.

@kevinrambaud
Copy link

You can define your route using the double colon notation and a switch statement to execute the appropriate logic based on your action.

app.put('/tasks/:id::action', function (req, res) {
  switch (req.params.action) {
    case "delete":
      return res.send("deleted")
    case "update":
      return res.send("updated")
    default:
      return res.send("invalid action")
  }
})

@patwhite
Copy link

@kevinrambaud Do you know if there's a way to do this without having to do a match on the command itself? So, if I wanted to have actual physical routes for the different actions (/tasks/1:publish) without having the sub-routing logic?

@patwhite
Copy link

Through trial and error, and from the express routing document guidance for treatment of $, I ended up with the following that appears to work:

app.put('/tasks/:id[:]action', function (req, res) {});

If the dev team has any comment if that's the best way, that would be great, but seems to work. Thanks!

@jonchurch
Copy link
Member

jonchurch commented Feb 27, 2020

@patwhite You might want to open an issue at https://github.com/pillarjs/path-to-regexp to get a more definitive answer.

@gudh
Copy link

gudh commented Oct 2, 2022

Well the expressJS docs addressed this

If you need to use the dollar character ($) in a path string, enclose it escaped within ([ and ]). For example, the path string for requests at “/data/$book”, would be “/data/([\$])book”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants