When making an AJAX call from the frontend to your Express.js backend, you encounter a CORS related error where your AJAX call is not successful. Here’s how to fix it:

//From where are request allowed.  Having '*' means from anywhere
res.set("Access-Control-Allow-Origin", "app.abc.com");

//Check if the method type sent in the request is of type "OPTIONS" (pre-flight)
if(req.method === "OPTIONS") {
    //What HTTP method(s) are allowed for this endpoint
    res.setHeader("Access-Control-Allow-Methods", "POST");

    //You can also add "Authorization" with "Content-Type" if you will be using authorization through the header
    res.setHeader("Access-Control-Allow-Headers", "Content-Type");

    //How long should the result of the pre-flight request be cached
    res.set("Access-Control-Max-Age", "3600");

    //204 means the request has been successfully done, but there will be no response
    res.status(204).send("");
}