0👍
✅
After a few reloads the original code worked, from(Heroku use Options instead of POST method)
Thank you @Dipten
One thing to watch for is in :
app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
I use my API for multiple apps and I need to have all of them listed for CORS in the code above.
const app = express();
app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
app.use(express.json());
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
// handle OPTIONS method
if ('OPTIONS' == req.method) {
return res.sendStatus(200);
} else {
next();
}
});
0👍
Handle option method work only without cors module.
As you use cors module so you need to make following changes.
const app = express();
app.use(cors({ origin: ['https://forms.ccenttcs.com/', 'http://localhost:9002'], }))
app.options('*', cors()) // include before other routes
if you face CORS error then change cors config by removing origin and test.
app.use(cors())
Source:stackexchange.com