I have a contact form that the user fills out which gets posted to my Web API to be sent using nodemailer
.
CORS works as expected with the when I run the client and API locally:
Client
localhost:4200 with POST request
sendEmail(queryObject: any) {
const body = JSON.stringify(queryObject);
const headers = new Headers();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/api/sendemail', body, {headers: headers})
.map((response: Response) => response.json());
}
API
localhost:3000
but as soon as I deploy my API to heroku and change the POST URL in the client to point to Heroku I get the preflight error:
OPTIONS https://keilcarpenter-portfolioapi.herokuapp.com/api/sendemail 503 (Service Unavailable)
XMLHttpRequest cannot load https://keilcarpenter-portfolioapi.herokuapp.com/api/sendemail. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 503.
I am sure I have CORS set up properly in server.js on my API:
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Why is CORS accepting the POST locally but not on Heroku?