Skip to content

Instantly share code, notes, and snippets.

@Vp3n
Created April 8, 2013 21:55
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Vp3n/5340891 to your computer and use it in GitHub Desktop.
Save Vp3n/5340891 to your computer and use it in GitHub Desktop.
Allowing CORS (Cross-Origin Resource Sharing) requests from grunt server
//Allowing CORS (Cross-Origin Resource Sharing) requests from
// grunt server, put this into Gruntfile.js
grunt.initConfig({
connect: {
livereload: {
options: {
port: 9000,
hostname: 'localhost',
middleware: function (connect) {
return [
function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
next();
},
];
}
}
}
}
});
@revolunet
Copy link

nice tip thanks :)

@paweloque
Copy link

I only get a:
Cannot GET /
with those settings. Any idea on what to do?

@adlenafane
Copy link

Same for me...

@digilist
Copy link

Thank you for the tip. I ran into the same problem as my predecessors.

I think, the configuration has changed in newer versions. With the following code, I got it to work:

grunt.initConfig({
  connect: {
    livereload: {
      options: {
        port: 9000,
        hostname: 'localhost',
        middleware: function(connect, options, middlewares) {
          middlewares.unshift(function(req, res, next) {
              res.setHeader('Access-Control-Allow-Origin', '*');
              res.setHeader('Access-Control-Allow-Methods', '*');
              next();
          });

          return middlewares;
        }
      }
    }
  }
});

@FerreroFacundo
Copy link

It didn't work for me, however, I found the answer here
gruntjs/grunt-contrib-connect#150
hope it helps !

@izinin
Copy link

izinin commented Nov 21, 2018

not working for me either : Cannot GET / is the only response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment