express
express copied to clipboard
Question: Is there a way to identify a route and create href link to it?
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 :)
Come on guys, what's up, not even a single comment? No one reading those issues?
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.
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 :)
+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')));
I created a PR for support for named routes in the Router project if anyone from this issue wants to comment - https://github.com/pillarjs/router/pull/36
Is there still no way to generate urls to routes instead of hardcoding paths all over the place?
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!
@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 https://github.com/pillarjs/router/pull/36, it also seems stalled, depending on https://github.com/pillarjs/router/pull/29 which is also stalled. Is this the router that express uses underneath?
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.