Cloud Stack Ninja

I have set up a server to handle a requests from react app and write data to databses. Very simple express.js server and all cors issues on localhost were solved with app.use(cors()) middleware. After i deployed server on heroku, i found that this issue occurs only when i try to write to database or read from it sending POST request.

Used database is heroku Postgres. Server - express.js.

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const knex = require('knex');
const db = knex({
   client: 'pg',
   connection: {
      connectionString: process.env.DATABASE_URL,
      ssl: true
   }
});

const app = express();

app.use(cors());
app.use(bodyParser.json());

app.get('/', (req, res) => {
   res.send('Server working just fine')
});

app.post('/saveDataToDatabase', (req, res) => {

   ///// Destructuring req.body /////
   console.log(req.body);

   const { nadal_year,
      mondaybreakfast,
      mondayvitamin,
      mondaylunch,
      mondayoode,
      tuesdaybreakfast,
      tuesdayvitamin,
      tuesdaylunch,
      tuesdayoode,
      wednesdaybreakfast,
      wednesdayvitamin,
      wednesdaylunch,
      wednesdayoode,
      thursdaybreakfast,
      thursdayvitamin,
      thursdaylunch,
      thursdayoode,
      fridaybreakfast,
      fridayvitamin,
      fridaylunch,
      fridayoode,
      created_in } = req.body;

   ///// ===================== /////

   db('nadalamenu')
      .returning('*')
      .insert({
         nadal_year: nadal_year,
         mondaybreakfast: mondaybreakfast,
         mondayvitamin: mondayvitamin,
         mondaylunch: mondaylunch,
         mondayoode: mondayoode,
         tuesdaybreakfast: tuesdaybreakfast,
         tuesdayvitamin: tuesdayvitamin,
         tuesdaylunch: tuesdaylunch,
         tuesdayoode: tuesdayoode,
         wednesdaybreakfast: wednesdaybreakfast,
         wednesdayvitamin: wednesdayvitamin,
         wednesdaylunch: wednesdaylunch,
         wednesdayoode: wednesdayoode,
         thursdaybreakfast: thursdaybreakfast,
         thursdayvitamin: thursdayvitamin,
         thursdaylunch: thursdaylunch,
         thursdayoode: thursdayoode,
         fridaybreakfast: fridaybreakfast,
         fridayvitamin: fridayvitamin,
         fridaylunch: fridaylunch,
         fridayoode: fridayoode,
         created_in: created_in
      })
      .catch(err => {
         console.log(err);
      })

});

app.post('/getDataFromDatabase', (req, res) => {

   db.select().table('nadalamenu').where('nadal_year', req.body.nadal_year).then(data => {
      res.send(data[data.length - 1]);
   });

});

app.listen(process.env.PORT || 3003, () => {
   console.log(`app is running on port ${process.env.PORT}`);
});

Server URL: https://tllp-sookla-api.herokuapp.com/

What can possibly be the reason?



Read more here: https://stackoverflow.com/questions/64400453/cors-issue-only-on-post-request-and-only-on-heroku

Content Attribution

This content was originally published by Ivan Lastovko at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: