Grunt connection server return cannot get / route with trunk

I have a connection server running on localhost, and in my base application, if I reload the route, tell localhost: 8000 / fun, which the server returns. GET / fun is not possible because / fun does not exist.

Somehow the server must know in order to serve index.html / fun or something else. I really tried this, but I have another error. Has anyone dealt with this before?

TL; DR Cannot get / fun

+8
javascript gruntjs grunt-contrib-connect
source share
2 answers

So, the main solution was found here.

You want modRewrite:

npm install connect-modrewrite --save-dev 

And in your Grunt file:

 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; } } } } 
+8
source share

The accepted answer didnโ€™t work anymore (2015-10-20), because there were changes to connect the project structure, and connect.static was moved to its own โ€œserve-staticโ€ package. Therefore, you need to adapt this answer as follows:

 npm install serve-static --save-dev 

it is required in the Gruntfile.js file

 var serveStatic = require('serve-static'); 

and then change the middleware code to the following:

 middleware: function(connect, options) { var middlewares; middlewares = []; middlewares.push( modRewrite( ['^[^\\.]*$ /index.html [L]'] ) ); options.base.forEach( function( base ) { return middlewares.push( serveStatic( base ) ); }); return middlewares; } 

Otherwise, it works great! Helped me a lot!

+3
source share

All Articles