Comments (8)
@frogcjn As mentioned, it's not really possible. You can use the output of path-to-regexp
, of course, but you have to remember that it only outputs a RegExp and any matches will be all numeric when used with the existing router (no params population based on the name).
from path-to-regexp.
I don't think so, but cc @dougwilson
There's some things in router
that will help eventually with this, such as the 2.0 router using the new path-to-regexp
and having a plugable path engine, but for now I don't think there's a way.
from path-to-regexp.
Yes, in the current router, there is not a simple way to use an updated version of path-to-regexp
except wait for router@2
or express@5
to come out with the updated module.
from path-to-regexp.
How can I use the latest version path-to-regexp with express 4?
from path-to-regexp.
Is this the right way?
npm install path-to-regexp --save
(v1.5.3)
import * as pathRegexp from "path-to-regexp" // v1.5.3
// for path "/signup:phone"
router.post(pathRegexp("/signup\\:phone"), wrap(async (req, res) => {
return res.json({ success: true, message: "Success!"})
}))
from path-to-regexp.
Ultimately, it seems that the keys need to go into the regexp's "keys" property, but that is being overwritten with numeric keys. So you could use defineProperty to make it have the correct value and be readonly (with setter to silently ignore updates, not throw an exception) before you pass it to express's .use .post .get etc.
It's a bit of a weird workaround tho.
EDIT: Here's a fiddle illustrating the hack:
https://jsfiddle.net/3e9crswm/
EDIT 2: Well, cancel this. It seems express does not use the regexp's keys property after all, but uses the original keys array that it passed to pathToRegexp. So this whole stunt is in vain.
from path-to-regexp.
I think it might be a good idea to change regexpToRegexp to respect any existing keys property instead of overwriting it. It could even be backported to an old path-to-regexp version that express can use, but will be a good feature without that too.
from path-to-regexp.
Closing as resolved
from path-to-regexp.
Related Issues (20)
- Regression > 0.1.7 HOT 4
- regex match fail HOT 3
- Double decoding path parameters HOT 17
- Publish to NPM HOT 1
- Next dynamic route with spread (...) throws 'Unexpected MODIFIER at 70, expected END' HOT 2
- end option does not work as expected HOT 2
- When parsed to tokens, why are backslashes stripped from pattern? HOT 2
- Invalid regex patterns not properly detected. HOT 2
- Another dumb question: How to run tests? HOT 2
- Erroneous test result. HOT 3
- `?` in optional capture group isn't connected to group in output - named param HOT 11
- SyntaxError: Invalid regular expression: invalid group specifier name HOT 1
- The validate config in options is not work in compile function. HOT 1
- throw new TypeError(`Unexpected ${nextType} at ${index}, expected ${type}`) HOT 3
- How about match an empty array? HOT 1
- Regex for non-negative integers only matches two or more digits HOT 1
- Return the matched path/regex
- Escaping the question mark for a query string throws and error HOT 2
- Checking if a path is a subset of another path HOT 1
- params are being joined without `/` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from path-to-regexp.