Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grunt connect server returns cannot GET /route with backbone

I have a connect server running on localhost, and in my backbone app, if I reload a route, say localhost:8000/fun the server returns Cannot GET /fun obviously because /fun doesn't exist.

Somehow the server needs to know to serve index.html/fun or something. I actually tried that, but I get some other error. Has anyone dealt with this before?

TL;DR Cannot GET /fun

like image 920
dezman Avatar asked Jan 17 '14 00:01

dezman


1 Answers

So the basic solution was found here.

You want modRewrite:

npm install connect-modrewrite --save-dev

And in your Gruntfile:

modRewrite = require('connect-modrewrite')

Coffee:

connect:
  server:
    options:
      port: 8765
      open: true
      base: ['./']
      middleware: (connect, options) ->
        middlewares = []

        middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']))
        options.base.forEach( (base) ->
          middlewares.push(connect.static(base))
        )
        middlewares

Vanilla JS:

connect: {
  server: {
    options: {
      port: 8765,
      open: true,
      base: ['./'],
      middleware: function(connect, options) {
        var middlewares;
        middlewares = [];
        middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']));
        options.base.forEach(function(base) {
          return middlewares.push(connect["static"](base));
        });
        return middlewares;
      }
    }
  }
}
like image 82
dezman Avatar answered Oct 23 '22 12:10

dezman