Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Jul 26, 2013

nice tip thanks :)

@paweloque

This comment has been minimized.

Copy link

commented Aug 13, 2014

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

@adlenafane

This comment has been minimized.

Copy link

commented Aug 16, 2014

Same for me...

@digilist

This comment has been minimized.

Copy link

commented Sep 17, 2014

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

This comment has been minimized.

Copy link

commented Mar 13, 2015

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

@izinin

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.