Search code examples
angularjsnode.jsgruntjscorsgrunt-contrib-connect

Getting Error POST http://localhost:8080/stub/cms/myalerts2.json 405 (Method Not Allowed)


I am running my application with grunt task runner, it throws me following error:

POST http://localhost:8080/stub/cms/myalerts2.json 405 (Method Not Allowed)

I have tried following things in my Gruntfile.js

 connect: { 
                server: { 
                    options: { 
                        keepalive: true, 
                        port: 8001, 
                        protocol: 'http', 
                        hostname: '*', 
                        base: 'dis', 
                        directory: 'dis', 
                        open: { 
                            target: 'http://localhost:8001/mydemo.html', 
                            appname: 'open' 
                        },

                        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,POST,DELETE');
                            res.setHeader('Access-Control-Allow-Headers', req.headers['access-control-request-headers']);
                        });
                        return middlewares;
                }   
} 
        } 
    },

Still its not allowing me to run. Any help or suggestion will me most welcome. Thanks in advance


Solution

  • After couple of irritating days finally my code works for me. Look into my code below:-

    connect: { 
                server: { 
                    options: { 
                        keepalive: true, 
                        port: 8001, 
                        protocol: 'http', 
                        hostname: '*', 
                        directory: 'dist', 
                        open: { 
                            target: 'http://localhost:8001/myDemo.html', 
    
                        },
                            middleware: function(connect, options, middlewares) {
    
                                    middlewares.unshift(function(req, res, next) {
                                        res.setHeader('Access-Control-Allow-Credentials', true);
                                        res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                                        res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
                                        **if (req.method.toUpperCase() == 'POST') req.method='GET';**
                                        return next();
                                    });
    
                                    return middlewares;
                            }
    
                    } 
                } 
            },
    

    see the star marked line ie if (req.method.toUpperCase() == 'POST') req.method='GET'; i done this trick and its worked for me. This aricle also helps to me https://github.com/gruntjs/grunt-contrib-connect#middleware