3

I've a app for which I need to change some url. For example localhost:9000 should look like localhost:9000/myapp

All the static files will have url in html like myapp/style/main.css. But in actual myapp folder will not exist, I just need to show it in url. I don't know how to internally rewrite it.

Server is "grunt-contrib-connect"

Pal Singh
  • 1,964
  • 1
  • 14
  • 29

1 Answers1

3

use http-rewrite-middleware

After you install it, put this at the top of your Gruntfile

var rewriteModule = require('http-rewrite-middleware');

Then under your connect livereload do something like this:

livereload: {
    options: {
      open: 'http://localhost:9000/myapp',
      middleware: function(connect, options, middlewares) {

        // rewrite (make sure it is first)
        middlewares.unshift(rewriteModule.getMiddleware([
          {from: '^/myapp/(.*)', to: '/$1'}
        ]));

        //paths
        middlewares.push(connect.static('.tmp'));
        middlewares.push(connect().use(
          '/bower_components',
          connect.static('./bower_components')
        ));
        middlewares.push(connect.static(appConfig.app));

        return middlewares;
      },
bpaul
  • 1,949
  • 1
  • 13
  • 23
  • Alright, It works well. Now next problem is that static files are not loading because they are using /myapp/ in there url to load and they get a nice 404. – Pal Singh Jan 17 '15 at 05:55