gstroup / apimocker

node.js module to run a simple http server for mock service responses.
MIT License
280 stars 81 forks source link

Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response #54

Closed AshishPashap closed 8 years ago

AshishPashap commented 8 years ago

Hi,

We are facing an issue - XMLHttpRequest cannot load http://localhost:7879/myApp/myService/v1/account. Method PATCH is not allowed by Access-Control-Allow-Methods in preflight response.

I tried adding CORS module and also tried adding response headers in my mock server but it didn't work, later found that adding the PATCH in apimaker.js resolved the issue.

node_modules\apimocker\lib\apimocker.js should have res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,PATCH,DELETE'); instead res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');

Is there specific reason for not supporting PATCH ? without PATCH it works fine from postman but not working from our application.

It would be great if this could be resolved.

thanks, Ashish Pashap

AshishPashap commented 8 years ago

I have tried adding custom header as suggested in document section - "Adding custom middleware" as below

    var customMiddleware = function(req, res, next) {
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,PATCH,DELETE');
        console.log("Set custom headers for "+req.method);
        next();
    };
    mocker= apimocker.createServer().setConfigFile("test/test-config.json");
    mocker.middlewares.unshift(customMiddleware);
    mocker.start();

_Here are the console logs in executed order : _

I believe calling/setting the custom headers should be in another way like first it should set from the lib/apimocker.js and if any custom header set then it should overwrite.

Please suggest me on the same.

thanks, Ashish Pashap

gstroup commented 8 years ago

Fixed in version 0.4.12.

AshishPashap commented 8 years ago

wow.great, thanks a lot.