Home
Support Forums

CORS errors in production but locally works flawlessly

fdc499aa-4695-4915-9c60-57c8cef5ec12 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongooseServerSelectionError: Server selection timed out after 30000 ms","reason":{"errorType":"MongooseServerSelectionError","errorMessage":"Server selection timed out after 30000 ms","message":"Server selection timed out after 30000 ms......."

2:53:44 PM: Duration: 39.66 ms Memory Usage: 115 MB
2:53:44 PM: Unknown application error occurred

ERROR (node:8) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

Hey can anyone help me out ive been running into random CORS errors on all my netlify apps.
my routes and request will work like 90% of the time then randomly ill get those errors popping up.
My apps work flawlessly while working in localhost environment when im testing my app in production i get CORS errors randomly and i dont know when or why they happen. My friend also uses the same setup as me and doesnt run into these problems.

This is how im setting my CORS UP

const cors = require(“cors”);

const corsOptions = (whitelist) => {

return {

origin: function (origin, callback) {

  if (whitelist.indexOf(origin) !== -1) {

    callback(null, true);

    console.log("whitelist", whitelist);

  } else {

    callback(new Error("Not allowed by CORS"));

    console.log("whitelist", whitelist);

  }

},

credentials: true,

};

};

module.exports = { cors, corsOptions };

var whitelist = “https://typegram.netlify.app”;

router.use(cors(corsOptions(whitelist)), (req, res, next) => {

res.header(

"Access-Control-Allow-Headers",

"Origin, X-Requested-With, Content-Type, Accept"

);

if (req.method === “OPTIONS”) {

res.header("Access-Control-Allow-Methods", "GET, PUT, POST, PATCH, DELETE");

return res.status(200).json({});

}

console.log(“cors fired”);

next();

});

Im not sure if i need anything else specific to netlify to avoid these errors?

hi there, first, let me ask a few q’s - are you trying to run mongoose, or mongo, or spinning up a server of some kind on netlify?

that’s not possible, actually -

Here is a good summary of what you can and can not do on Netlify you might find helpful: