7

Running yeoman server does not seem to recognize the .htaccess file by default. Is there an additional step to enable reading of the .htaccess file?

Here are the lines I have uncommented with no apparent affect in setting headers after restarting:

    # ----------------------------------------------------------------------
    # Cross-domain AJAX requests
    # ----------------------------------------------------------------------

    # Serve cross-domain Ajax requests, disabled by default.
    # enable-cors.org
    # code.google.com/p/html5security/wiki/CrossOriginRequestSecurity

    <IfModule mod_headers.c>
      Header set Access-Control-Allow-Origin "*"
    </IfModule>

Or maybe the proper question is how would one set headers when running yeoman server? Is there another option, perhaps in Gruntfile.js?

Michael Allan Jackson
  • 4,217
  • 3
  • 35
  • 45

1 Answers1

22

grunt server is just a Node.js connect server and doesn't support .htaccess

Though since grunt-contrib-connect supports custom middlewares you can add this to your Gruntfile.js:

var corsMiddleware = function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  next();
}

grunt.initConfig({
  connect: {
    server: {
      options: {
        middleware: function(connect, options) {
          return [
            // Serve static files
            connect.static(options.base),
            // Make empty directories browsable
            connect.directory(options.base),
            // CORS support
            corsMiddleware
          ];
        }
      }
    }
  }
});
Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
  • Any updates on Yeoman 1.0 and how to support Connect middleware? – adardesign May 05 '13 at 14:55
  • Thanks! Can you please provide a link or explain the options of the corsMiddleware and how to use it to mimic a traditional .htaccess file settings.. Especially for using it as a front end ajax mock api server – adardesign May 05 '13 at 17:17
  • corsMiddlware is just a custom middleware I created for you. No options. Middlewares are simple proxies that lets you modify requests. In this example it just adds some headers. But you can do even more powerful things. I suggest you read up on Connect middlewares: https://google.com/search?q=connect+middleware – Sindre Sorhus May 05 '13 at 19:05
  • Is this added in the root Gruntfile.js or in the node_modules/grunt-contrib-connect/Gruntfile.js? I have tried both, but no success. I am trying to connect to MongoHQ from my development machine. – pelachile May 25 '13 at 05:25
  • 1
    It should be added to *your* Gruntfile.js (root). – Sindre Sorhus May 26 '13 at 23:22
  • It seems yeoman 1.0 support CORS "*". I run `grunt server` and without and config I can make requests to different domain. – Lukasz Madon Dec 16 '13 at 15:20
  • I get `Warning: connect.static is not a function Use --force to continue` – URL87 Jul 15 '16 at 02:25