I don't know what is causing this error. I set all the right headers and all the correct methods. This is my Node Js Server:
Full Error : login:1 Access to XMLHttpRequest at 'http://localhost:3000/' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. zone-evergreen.js:2845 POST http://localhost:3000/ net::ERR_FAILED
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.setHeader(
"Access-Control-Allow-Methods",
"GET,PUT, POST, PATCH, DELETE, OPTIONS"
);
next();
});
I am sending POST request from an Angular app from http://localhost:4200 and connecting it with a MongoDB Server.
If someone could help me with this it would be great.