Getting trunk and express routes that play beautifully

I have an express application serving the following routes.

/login /signup / /app 

And I want my main application to manage the following routes.

 /app/overview/:company_id/:date /app/rooms/:company_id/:date 

In any case, the express serves the basic app.jade at / app and then is supposed to pass the navigation to the trunk, but instead it intercepts and gives me a route that was not found.

How can I do it? if someone can copy and paste the url like

 /app/overview/3/2012-12-12 
+4
source share
2 answers

Perhaps you have two related problems: you use push state client-side (that is, you use "real" URLs, not hash fragments like / app # overview), but (maybe) did not enable push state in Backbone, and your express configuration does not respond to these client routes.

Using push status URLs, you still have to process these server-side URLs, as the user can visit your site using these direct client-side URLs (something that doesn't happen with hash fragments, since the hash part is never sent to the server).

So, to fix server-side processing, express allows you to use regular expressions as routes, so instead of:

 app.get('/app', function(req, res) { // Render the app... }); 

You can do (see here ):

 // The regexp could be a little more restrictive, obviously, but you get the point... app.get(/^\/app(\/\w+)*$/, function(req, res) { // Render the app... }); 

Thus, no matter what URL / application / * is used as an entry point to your web application, it receives the necessary content.

Then, on your side of the baseline initialization, you must start history management with Backbone.history.start({pushState: true}) to activate the push state. See here .

+4
source

You can simply serve the application, leaving the route untouched.

 // given your app serves routes.index app.get('/app/overview/:company_id/:date', routes.index); app.get('/app/rooms/:company_id/:date', routes.index); // ... and so on 

Thus, the application will be serviced as usual, allowing the trunk to process the route.

+2
source

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


All Articles