0

So I don't use any backbone logic until the user logs into the app. So for using node.js the application starts on localhost:3000 and the server handles the routes in this portion. So for example after visiting localhost:3000 there are two options, signup, or login which express handles the engineering of this, and a simple routes.js file handles the logic for these routes. /signup, /login

That part is all server side, no need for backbone.js at this point in my opinion.

Then finally the user successfully logs into the app, which starts in /profile that is where I include backbone and my javascripts, here is where I rely more on client side routing and logic because the user is approved to play, and is granted access.

Now being kind of noobish here, but my question is since my app starts inside /profile why does the router get jacked up? For example I have a chat route and to invoke this route the url doesn't respect a trailing slash localhost:3000/profile#chat works but not this localhost:3000/profile/#chat

I read this Backbone.js route optional parameter I tried reversing the syntax but no luck.

Here are my routes

module.exports = Router = Marionette.AppRouter.extend({
    appRoutes: {
        ''  : 'home',
        '(/:)chat' : 'chat' // tried playing around here
    }
});

So since starting on the /profile route created by the server how do I get a trailing slash to work?

laser
  • 1,388
  • 13
  • 14
Michael Joseph Aubry
  • 12,282
  • 16
  • 70
  • 135

1 Answers1

0

Backbone is a frontend JS framework completely separate from your node/express backend. They don't share any logic as far as routing is concerned.

Use express to serve the /profile route:

app.get('/profile', function(req, res){
  res.render('profile', {somedata:'somevalue'}); // etc..
});

Then on the fronend, your Backbone.Router handles any routes from there on:

var myRouter = Backbone.Router.extend({
  routes: {
    '/' : 'profile', // Actually '/profile/
    '/chat' : 'chat' // Actually '/profile/chat
  }
});

Lastly, remember to call Backbone.history.start() after your router and all routes have been defined to start monitoring for hash changes.

nordhagen
  • 799
  • 5
  • 18
  • Thanks I appreciate it, the example above looks like it should work. I get that defining the home/profile route with a '/' should be the answer, but it's not working for some reason. – Michael Joseph Aubry Mar 25 '14 at 23:30
  • I think it may be because the script is not getting included.. Hold on let me do some debugging. I feel like this should work. – Michael Joseph Aubry Mar 25 '14 at 23:32
  • Yeah it seems like on that route I am getting 404 errors for my scripts and css. Express has to be confusing my application. – Michael Joseph Aubry Mar 25 '14 at 23:36
  • My script is in `js/myapp.js` http is requesting `/profile/js/myapp.js` – Michael Joseph Aubry Mar 25 '14 at 23:37
  • Okay so I got it to work, but not with the trailing slash on the chat route. When I add `'/chat': 'chat'` this tells express or node or whatever to get my files at `profile/js/myapp.js`.. When `'chat': 'chat'` allows this `/profile#chat` and gets my files appropriately. – Michael Joseph Aubry Mar 25 '14 at 23:54