Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Generation for Routes in Express

I'm considering using Express framework in my next node.js project. However, a stumbling block for me is non-existence of URL generation for routes like in most other non-Sinatra based frameworks, examples- Django, Flask, Rails etc.

I tried looking for some Connect middleware to serve my task and I did find Barista, Escort, Sherpa and the likes but looking at their GitHub pages, all seem dead and in-active. So, I don't want to go for something that's not maintained anymore for obvious reasons.

My main concern here is that the project may get really large and it WILL be a pain to update URLs in every page whenever business and/or aesthetic requirements change.

Is there something that I failed to see in the docs/tests? If not, then how do I extend the routing framework in Express to do URL generation and make this wrapper available in my views as well as controller functions?

UPDATE: (22/3/2012) I found this page: https://github.com/clyfe/tweet_express/wiki/TODO which specified some routers that do URL generation and stumbled upon the escort router which can also interface with express.

like image 497
Ishbir Avatar asked Jan 06 '12 07:01

Ishbir


People also ask

How get full URL in Express?

Getting the full URL of a Request in Express # Within a route, we can use the req object to get the full URL. The full URL is composed of a few different pieces: the protocol of the current URL. the host of the URL (i.e. the domain name)

How do you create routes in an Express application?

To use the router module in our main app file we first require() the route module (wiki. js). We then call use() on the Express application to add the Router to the middleware handling path, specifying a URL path of 'wiki'.

How can we create Chainable route handlers for a route path in Expressjs app?

By using app. route() method, we can create chainable route handlers for a route path in Express.

What does Express router () use do?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.


2 Answers

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

Example from the 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})
like image 122
antitoxic Avatar answered Oct 01 '22 22:10

antitoxic


You might try Locomotive, which is built on Express.

It does much more than route generation. From the docs: "Locomotive brings additional MVC-based structure, for architecting larger applications, while leveraging the power of Express and Connect middleware."

Locomotive's router generates helpers that are automatically available to controllers and views.

like image 33
Luke Vivier Avatar answered Oct 01 '22 21:10

Luke Vivier