I have a node.js backend, and in my server.js file I have this:
const app = express();
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
It is working correctly in the Localhost, but after uploading to the server, it gives this Cors error:
I search for a solution and configure IIS by web.config file to resolve Cors error, I add this line to the web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
After that, the error message changed to this:
I returned and deleted the cors lines in the server.js but than it reverses at the beginning, the first same error message again. How can I solve this?
const app = express();
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Methods",
"OPTIONS, GET, POST, PUT, PATCH, DELETE"
);
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
next();
});
It is working correctly in the Localhost, but after uploading to the server, it gives this Cors error:
I search for a solution and configure IIS by web.config file to resolve Cors error, I add this line to the web.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
After that, the error message changed to this:
I returned and deleted the cors lines in the server.js but than it reverses at the beginning, the first same error message again. How can I solve this?