How to use colon as normal character instead of route param

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.

Author: Fantashit

2 thoughts on “How to use colon as normal character instead of route param

  1. 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")
      }
    })
  2. 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!

Comments are closed.