Backbone Router : Get rid of # in the URL

2019-02-16 02:57发布

问题:

I'm getting backbone router to work

App.Router = Backbone.Router.extend({
      routes: {
        "todo": "todo"
      },
      todo: function() {
        alert(1);
      }
});
Backbone.history.start();

This works well when I goto url : domain:port/#/todo

I want this to work without # in the URL, I tried putting pushState parameter as mentioned in the documents.

    Backbone.history.start({pushState: true});

This simply redirects # url to the non hashed one

domain:port/#/todo (redirects to ==>) domain:port/todo

But when I visit this URL directly

domain:port/todo

it doesn't work: "Cannot GET /todo".

Is there any way, I can make this URL work without # in it?

回答1:

You need to write server side code to deliver the page for the URL.

Using pushState just tells the browser "I have used JS to change the content of the page so it matches what you would get if you ask for this URL".

It doesn't cause the content to match that URL, you have to do that as a separate task.