Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yeoman how to set headers (.htaccess?)

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?

like image 709
Michael Allan Jackson Avatar asked Jan 31 '13 01:01

Michael Allan Jackson


1 Answers

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
          ];
        }
      }
    }
  }
});
like image 153
Sindre Sorhus Avatar answered Sep 22 '22 03:09

Sindre Sorhus