node.jsexpress

How to allow ':' character in Node.js Express routes (Google custom methods)


I found a Google "recommendation" about REST custom methods, that seems to me very useful

https://cloud.google.com/apis/design/custom_methods

How to allow the ':' character in Node.js routes without matching it as a named parameter?

Tried with something like

app.post('/item\:method'

but this has strange results, this matches everything starting with /item ...

Also tried with

app.post('/item\\:method'

but I get

SyntaxError: Invalid regular expression: /^\/item\(?:([^\/]+?))\/?$/i: Unmatched ')'


Solution

  • To allow the colon (:)enter code herein Node.js routes without treating it as a parameter, you can escape the colon using a double backslash.

    app.post('/item[:]method', (req, res) => {
    // Handle request here
    

    });