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

Question: Is there a way to identify a route and create href link to it? #2739

Open
fourpixels opened this issue Aug 21, 2015 · 9 comments
Open

Comments

@fourpixels
Copy link

Hi there,
I'm using Express for what I imagine to be a large project, and I have many href links inside my views. I do not want to hardcode them at all! Any time I change something I must go and search in the code in order to make it work.

Short story: how can I track back a route, so I can use this in views:

<a href="{{ url('route_id') }} /> <!-- // as you don't mind what the template engine is -->

Long one: I've searched for a day about routing in Express and I found a few "reverse" routing packages, ALL of them quite broken - they either don't work with Router() or they create new Router every time, etc.
I got to know all the Layers and stacks that the Routing part creates. I understand that you guys want order in routes, as well as multiple handlers for the same path. I agree with all that.

But isn't there some easy way to add ids for those routes? I found that if I simply use

app.use('/path', function namedFunction(req, res, next) {});

I get namedFunction as a name of the Layer in the stack, which is great. But if I use router (like router.route('/path').get(...).post(...), then the Layer is named router and inner stack contains anonymous functions.

My idea here was that after I add all routes, I can browse app._router and it's stacks, getting names of routes, then using a function with name parameter so I can construct back a route.

Am I missing something, or there is no way to make this easy working? I can't imagine someone having 100+ routes in a project, that hardcodes their links inside it - I just refuse to think of it :)

@fourpixels
Copy link
Author

Come on guys, what's up, not even a single comment? No one reading those issues?

@dougwilson
Copy link
Contributor

Hi! Yes, currently it's not possible, especially when you start to think about how we actually allow your middleware to alter req.url/req.method and we continute to route based on these new values! Basically, the way our router is written, you cannot actually figure out the destination without executing the stack with a real request.

I believe, though, there are third party modules (I think part of kraken?) than provide this functionality, though.

@dougwilson dougwilson self-assigned this Aug 27, 2015
@fourpixels
Copy link
Author

Thanks for the answer, @dougwilson ! I've just seen their enrouten package. It does some pretty magical stuff that I'll have to deeply investigate tomorrow and see if I can use. Thanks for that!

Otherwise - is it really that bad idea to check if one of the params passed to the router is String (as they are all now checked to be Function), and if so - set it as a name of the Layer? Because now the name is set to the function name (which is sometimes undefined) and moreover the nested layers are all anonymous I guess because of this bind, right? I guess if it's because of it, there should be some pretty simple solution to at least set proper name to the router, and let people like me browse them however they like :)

@diosney
Copy link

diosney commented Sep 1, 2015

+1 to this, will allow nested routes a breeze too.

Currently I'm doing the following, which is not the most pretty thing out there but it works :) (I tested several reverse packages too and didn't liked the results):

global.erp.app.set('route:organizations', global.erp.app
    .route(global.erp.config.get('api:prefix') + '/organizations')
    .get(require('./routes/index'))
    .post(require('./routes/create')));

@sjanuary
Copy link
Contributor

I created a PR for support for named routes in the Router project if anyone from this issue wants to comment - pillarjs/router#36

@edevil
Copy link

edevil commented Aug 24, 2018

Is there still no way to generate urls to routes instead of hardcoding paths all over the place?

@wesleytodd
Copy link
Member

Hi @edevil, I think you should go read the conversation on that PR linked by @sjanuary. I have not seen any finalizing progress on it, so if you can re-ignite the conversation in a productive way that would be great!

If I can give some feedback, I think your approach here could use some refining. It is usually better to only comment when you have something constructive to add to the conversation, so a better way to phrase the sentiment maybe would be "Is there anything I can do to help get this feature?". That way we as the maintainers would be very willing to help point you in the right direction, as I did above. Happy contributing!

@edevil
Copy link

edevil commented Aug 24, 2018

@wesleytodd I didn't mean to sound dismissive. Since I am new to the Node ecosystem and this was an open ticket with more than 2 years without activity, I really thought that there was something more and it had simply been forgotten.

Regarding pillarjs/router#36, it also seems stalled, depending on pillarjs/router#29 which is also stalled. Is this the router that express uses underneath?

@wesleytodd
Copy link
Member

Hey, I understand it looking forgotten. It takes someone championing the topic to push things forward, sometimes pinging on a topic for a while to get people's attention. Yes pillarjs router is the express router, but for the next major release we are working on. This would be a great feature to get landed before then, so maybe you can start some conversations over there on how you think we can move it forward.

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

No branches or pull requests

7 participants