-1

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.

J Woodchuck
  • 2,265
  • 24
  • 44

1 Answers1

-1

There are several ways to fix/workaround this.

  1. Turn off CORS. For example: https://stackoverflow.com/questions/3102819/disable-same-origin-policy-in-chrome
  2. Use a plugin for your browser : https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
  3. Use a proxy such as nginx. http://nginx.org/en/docs/beginners_guide.html
  4. Go through the necessary setup for your server. This is more a factor of the web server you have loaded on your EC2 instance (presuming this is what you mean by "Amazon web service"). For your specific server you can refer to the enable CORS website.