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 newer versions with express? #66

Closed
georgir opened this issue Nov 19, 2015 · 8 comments
Closed

How to use newer versions with express? #66

georgir opened this issue Nov 19, 2015 · 8 comments
Labels

Comments

@georgir
Copy link

georgir commented Nov 19, 2015

Express 4.13.3 seems to depend on old path-to-regexp version 0.1.7
A lot of cool things that work in the express route tester turn out to not actually work in express, and I assume it is because of the version difference.
Is there a recommended way to get express to use the new versions, or should I just hack its package.json, or what?

@blakeembrey
Copy link
Member

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.

@dougwilson
Copy link

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.

@frogcjn
Copy link

frogcjn commented Jun 25, 2016

How can I use the latest version path-to-regexp with express 4?

@frogcjn
Copy link

frogcjn commented Jun 25, 2016

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

@blakeembrey
Copy link
Member

@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).

@georgir
Copy link
Author

georgir commented Jun 27, 2016

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.

@georgir
Copy link
Author

georgir commented Jun 27, 2016

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.

@jonchurch
Copy link
Member

Closing as resolved

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

No branches or pull requests

5 participants