gruntjs / grunt-contrib-connect

Start a static web server.
http://gruntjs.com
MIT License
714 stars 145 forks source link

Is POST supported? #97

Closed LostSenSS closed 10 years ago

LostSenSS commented 10 years ago

Hi! Why POST request can not work? Example : I send simple form on current page address. Output in Grunt console : [D] server POST /build/views/profile.html 404 - - 1 ms With GET all fine: D] server GET /build/views/profile.html?name=test 200 8960 - 1 ms If I try to do the same on real server then all right.

shama commented 10 years ago

Static only responds to GET requests. To handle other request methods, you would need to add your own middleware and check for req.method === 'POST': https://github.com/gruntjs/grunt-contrib-connect#middleware

kylerush commented 10 years ago

@shama i've tried your suggestion, but i can't get it to work. the server sends back a 405 status code.

code here: https://github.com/optimizely/marketing-website/blob/krush/oform/Gruntfile.js#L117

any ideas?

shama commented 10 years ago

@kylerush You might need to unshift your middleware instead of push. My guess is another middleware is grabbing your POST request first.

kylerush commented 10 years ago

@shama that worked. thanks for the help.

luozhihua commented 9 years ago

I got a 404 response error when I send request using POST method, this is my task config?

    connect: {
      livereload: {
        options: {
          middleware: function(connect, options, middlewares) {

            middlewares.unshift(function (req, res, next) {
              res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
              res.setHeader('Access-Control-Allow-Credentials', true);
              res.setHeader('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE');
              res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
              return next();
            });

            return [
              connect.static('.tmp'),
              connect.static('.tmp/concat'),
              connect().use('/bower_components'),
              connect.static('app')
            ];

          }
        }
      },
shama commented 9 years ago

@luozhihua https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

luozhihua commented 9 years ago

arr.unshift() doe's not accept a Function type arguments?

shama commented 9 years ago

Oh sorry @luozhihua I misread the code. The unshift usage there is correct, my bad.

But take a look at the array of middlewares you're returning vs the middlewares array you're unshifting. That might be the problem. ;)

mturnwall commented 8 years ago

I'm getting 405 error as well. I'm on the latest version, v1.0.2. I have middleware setup set the headers to allow POST but I still get the error. This makes it very hard to test since I can test my changes locally.

server: {
    options: {
        base: ['<%= config.dev %>', '<%= config.app %>'],
        open: true,
        middleware: function (connect, options, middlewares) {
            middlewares.unshift(function (req, res, next) {
                res.setHeader('Access-Control-Allow-Origin', '*');
                res.setHeader('Access-Control-Allow-Credentials', true);
                res.setHeader('Access-Control-Allow-Methods', '*');
                res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

                return next();
            });
            return middlewares;
        }
    }
}
dylanmensaert commented 7 years ago

@mturnwall having the same problem, how did you fix it?