smollweide / node-mock-server

File based Node REST API mock server
MIT License
255 stars 65 forks source link

CORS: Error during options request #142

Closed LeonardoSSev closed 4 years ago

LeonardoSSev commented 4 years ago

I'm trying to fetch data from a mocked response through a web app but I am getting the following message:

Access to XMLHttpRequest at 'http://localhost:3001/api/v1/alarme/nao-lido/2019-06-04' from origin 'http://localhost:8771' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

So, I looked in the terminal and I found the following message:

Error: ENOENT: no such file or directory, open '/home/lssev/dev/bancos/spi/spi-mock/mock/rest/alarme/#nao-lido#{date}/OPTIONS/mock/response.txt'
    at Object.openSync (fs.js:440:3)
    at Object.writeFileSync (fs.js:1265:35)
    at MockController.writeFile (/home/lssev/dev/bancos/spi/spi-mock/node_modules/node-mock-server/lib/Utils.js:153:6)
    at MockController._handleMockRequest (/home/lssev/dev/bancos/spi/spi-mock/node_modules/node-mock-server/lib/controller/MockController.js:203:9)
    at Layer.handle [as handle_request] (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/route.js:137:13)
    at next (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/route.js:131:14)
    at next (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/route.js:131:14)
    at next (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/route.js:131:14)
    at next (/home/lssev/dev/bancos/spi/spi-mock/node_modules/express/lib/router/route.js:131:14)

So, as I saw the "OPTIONS" in the terminal, I thought I should replicate the mocked response for the OPTIONS method. But even with the OPTIONS response mocked endpoint I am getting the CORS problem. At least, I am not getting the message in the terminal again. Just my application that still not being able to fetch the data when requesting through the OPTIONS method.

When I make the same request through Postman, it works. Just through my web application that I am getting into this problem. Any advices that I should follow?

My options are:


var mockServer = require('node-mock-server');
var path = require('path');

mockServer({
    'restPath': path.join(__dirname, '/rest'),
    'uiPath': '/',
    'title': 'Api mock server',
    'version': 1,
    'urlBase': 'http://localhost:3001',
    'urlPath': '/api/v1',
    'port': 3001,
    'contentType': 'application/json',
    'accessControlExposeHeaders': 'X-Total-Count',
    'accessControlAllowOrigin': '*',
    'accessControlAllowMethods': 'GET, POST, PUT, OPTIONS, DELETE, PATCH, HEAD',
    'accessControlAllowHeaders': 'origin, x-requested-with, content-type',
    'accessControlAllowCredentials': 'true',
    'headers': {},
    'open': true,
    'dirName': __dirname
});
LeonardoSSev commented 4 years ago

Well, I solve the problem by doing the following steps: