Creating Route URLs in Express

I am considering using Express Framework in the next node.js project. Nevertheless, the stumbling block for me is the lack of generating URLs for routes, as in most other infrastructures that are not based on sinatra, examples are Django, Flask, Rails, etc.

I tried to find Connect middleware to accomplish my task, and I found Barista, Escort, Sherpa and the like, but looking at their GitHub pages, everything looks dead and active. Thus, I do not want to go for something that is not supported for obvious reasons.

My main concern here is that the project can become really big, and it will hurt to update the URLs on every page when business and / or aesthetic requirements change.

Is there something that I have not seen in the docs / tests? If not, then how do I expand the routing structure in Express to create URLs and make this shell available to my views, as well as to the functions of the controller?

UPDATE: (03/22/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO , which lists some routers that generate URLs and stumbled upon an escort router, which also can interact with expression.

+16
source share
4 answers

You can try Locomotive , which is built on Express.

This is much more than route generation. From the docs: β€œThe locomotive brings an extra MVC-based structure for archiving large applications while leveraging the power of Express and Connect middleware.”

The locomotive router generates helpers that are automatically available to controllers and views.

+10
source

Or stick with the expression and use the reversable-router package.

Example from readme:

app.get('/admin/user/:id', 'admin.user.edit', function(req, res, next){ //... }); //.. and a helper in the view files: url('admin.user.edit', {id: 2}) 
+14
source

From @ weird answer :

There is no ready-made mechanism for this. However, you can mimic the Django style as follows: define a urls.js file that will contain an array of URLs. Start with:

myviews.js

 exports.Index = function( req, res, next ) { res.send( "hello world!" ); }; 

urls.js

 var MyViews = require( "mywviews.js" ); module.exports = [ { name : "index", pattern : "/", view : MyViews.Index } ] 

Now in app.js (or any other main file) you need to bind the URL to Express. For example, like this:

app.js

 var urls = require( "urls.js" ); for ( var i = 0, l = urls.length; i < l; i++ ) { var url = urls[ i ]; app.all( url.pattern, url.view ); }; 

Now you can define a custom helper (Express 3.0 style):

 var urls = require( "urls.js" ), l = urls.length; app.locals.url = function( name ) { for ( var i = 0; i < l; i++ ) { var url = urls[ i ]; if ( url.name === name ) { return url.pattern; } }; }; 

and you can easily use it in your template. Now the problem is that it does not give you a fancy mechanism for creating URLs, as in Django (where you can pass additional parameters to url ). Alternatively, you can change the url function and extend it. I don't want to go into details, but here is an example of using regular expressions (you should be able to combine them with ideas together):

JS Reverse URL Express Route (Django Style)

Please note that I posted a question, so I had the same problem a while ago. : D

0
source

Maybe it's better to use this

https://www.npmjs.com/package/locomotive

-1
source

Source: https://habr.com/ru/post/905283/


All Articles