Grunt connect + modRewrite

2.2k Views Asked by At

The question is fairly easy to explain. I currently have two versions of my connect task, one that works with a grunt-contrib-proxy and the other that works with modRewrite. But I need to use both.

In this way, if a certain selection of 'routes' are loaded: ['/login', '/ord'] I need to proxy the request, but if anything else is loaded, I need to redirect to /index.html. The reason for this, is that I am using backbone.js, so localhost:8000/fun is supposed to load the fun route in backbone.

All of the following connect tasks are working perfectly (on their own).

TL;DR How can I combine these connect tasks:

connect: {
  server: {
    options: {
      port: 9001,
      middleware: function(connect, options) {
        var middlewares, proxy;
        proxy = require('grunt-connect-proxy/lib/utils').proxyRequest;
        middlewares = [proxy, connect["static"](options.base), connect.directory(options.base)];
        return middlewares;
      }
    },
    proxies: [
      {
        context: ['/login', '/ord'],
        host: '10.10.1.13',
        https: false
      }
    ]
  }
}

connect: {
  server: {
    options: {
      port: 9001,
      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;
      }
    }
  }
}

Here they are in coffee if you prefer:

connect:
  server:
    options:
      port: 9001
      # keepalive: true
      middleware: (connect, options) ->
        proxy = require('grunt-connect-proxy/lib/utils').proxyRequest
        middlewares = [
          proxy
          connect.static(options.base)
          connect.directory(options.base)
        ]
        middlewares
    proxies: [
      {
        context: ['/login', '/ord']
        host: '10.10.1.13'
        https: false
      }
    ]

connect:
  server:
    options:
      port: 9001
      open: true
      base: ['./']
      middleware: (connect, options) -> (
        middlewares = []
        middlewares.push(modRewrite(['^[^\\.]*$ /index.html [L]']))
        options.base.forEach( (base) ->
          middlewares.push(connect.static(base))
        )
        middlewares
      )

Thanks a bagillion.

1

There are 1 best solutions below

0
On

This article was helpful in solving my issues.

Coffee Solution:

connect:
  server:
    options:
      port: 9001
      base: './'
      # keepalive: true
      uselessMiddleware: (req, res, next) ->
        next()
      middleware: (connect, options) ->
        # same as in grunt-contrib-connect

        directory = options.directory || options.base[options.base.length - 1]
        if (!Array.isArray(options.base))
          options.base = [options.base]

        # custom middleware
        proxy = require('grunt-connect-proxy/lib/utils').proxyRequest
        middlewares = [
          # proxy
          proxy
          connect.static(options.base[0])
          connect.directory(options.base[0])
          # mod rewrite
          modRewrite(['^[^\\.]*$ /index.html [L]'])
          connect.static(options.base[0])
        ]

        # same as in grunt-contrib-connect
        options.base.forEach( (base) ->
          middlewares.push(connect.static(base))
        )
        middlewares.push(connect.directory(directory))
        middlewares

    proxies: [
      {
        context: ['/login', '/UserData', '/logout']
        host: '10.10.1.13'
        https: false
      }
    ]