title: Deploy Node, Express, and MongoDB server to heroku
body: <p>I created a server using Node, Express, and MongoDB Atlas. Here is the code of this:</p>
  <p><strong>server.js:</strong></p>
  <pre><code>const express = require('express');
  const cors = require('cors');
  const mongoose = require('mongoose');
  
  require('dotenv').config();
  
  // model
  let Cards = require('./models/cards.model');
  
  // App config
  const app = express();
  const port = process.env.PORT || 5000;
  
  
  app.use(cors());
  app.use(express.json());
  
  
  // DB config
  const uri = process.env.ATLAS_URI;
  
  mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });
  
  const connection = mongoose.connection;
  
  connection.once('open', () =&gt; {
      console.log(&quot;MongoDB database connection has been established successfully.&quot;);
  })
  
  
  // API Endpoints
  
  // POST parameter
  app.post('/tinder/cards', (req, res) =&gt; {
      const card = req.body;
  
      Cards.create(card, (err, data) =&gt; {
          if(err){
              return res.status(500).send(err)
          } else {
              return res.status(200).send(data)
          }
      })
  })
  
  // GET parameter
  app.get('/', (req, res) =&gt; {
      return res.status(200).send(&quot;Welcome to Tinder Clone Backend with NodeJS, ExpressJS, and MongoDB!!!&quot;)
  })
  
  app.get('/tinder/cards', (req, res) =&gt; {
      Cards.find((err, data) =&gt; {
          if(err) {
              return res.status(500).send(err);
          } else {
              return res.status(200).send(data)
          }
      })
  });
  
  // Listener
  app.listen(port, () =&gt; {
      console.log(`Server is running on port ${port}`)
  });
  
  </code></pre>
  <p><strong>package.json</strong></p>
  <pre><code>{
    &quot;name&quot;: &quot;tinder-backend&quot;,
    &quot;version&quot;: &quot;1.0.0&quot;,
    &quot;description&quot;: &quot;&quot;,
    &quot;main&quot;: &quot;index.js&quot;,
    &quot;scripts&quot;: {
      &quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;
    },
    &quot;keywords&quot;: [],
    &quot;author&quot;: &quot;&quot;,
    &quot;license&quot;: &quot;ISC&quot;,
    &quot;dependencies&quot;: {
      &quot;cors&quot;: &quot;^2.8.5&quot;,
      &quot;dotenv&quot;: &quot;^8.2.0&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;mongoose&quot;: &quot;^5.12.2&quot;
    }
  }
  
  </code></pre>
  <p><strong>cards.model.js</strong></p>
  <pre><code>// creating database schema using mongoose
  
  const mongoose = require('mongoose');
  
  const Schema = mongoose.Schema;
  
  const cardsSchema = new Schema({
      name: String,
      imgUrl: String
  });
  
  
  
  const Cards = mongoose.model('Cards', cardsSchema);
  
  module.exports = Cards;
  </code></pre>
  <p>It is working locally and all endpoints sending and receiving data from mongodb atlas.
  when I run this command in terminal to start the local server:
  <strong>$ nodemon server</strong></p>
  <p>it is showing this:</p>
  <pre><code>[nodemon] 2.0.7
  [nodemon] to restart at any time, enter `rs`
  [nodemon] watching path(s): *.*
  [nodemon] watching extensions: js,mjs,json
  [nodemon] starting `node server.js`
  Server is running on port 5000
  MongoDB database connection has been established successfully.
  </code></pre>
  <p>This means that my local server is running well and when I check in my browser and type in my server link i.e <strong>http://localhost:5000/tinder/cards</strong>, it showing data that I have posted previously:</p>
  <pre><code>[
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f2&quot;,
  &quot;name&quot;: &quot;Elon Musk&quot;,
  &quot;imgUrl&quot;: &quot;https://s3.india.com/wp-content/uploads/2020/03/Elon-Musk-AP.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f3&quot;,
  &quot;name&quot;: &quot;Shakira&quot;,
  &quot;imgUrl&quot;: &quot;https://ichef.bbci.co.uk/news/976/cpsprodpb/738B/production/_116497592_gettyimages-971720370.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f4&quot;,
  &quot;name&quot;: &quot;Jeff Bezos&quot;,
  &quot;imgUrl&quot;: &quot;https://media.wired.com/photos/6019cab23453f789506008d0/master/pass/Sec_Bezos_1036084400.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f5&quot;,
  &quot;name&quot;: &quot;Dua Lipa&quot;,
  &quot;imgUrl&quot;: &quot;https://assets.vogue.com/photos/5f48136693122510d16f0352/4:3/w_1080,h_810,c_limit/118520052_586967341971321_6121798062289952442_n.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f6&quot;,
  &quot;name&quot;: &quot;Pitbull&quot;,
  &quot;imgUrl&quot;: &quot;https://vz.cnwimg.com/wp-content/uploads/2010/03/Pitbull.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f7&quot;,
  &quot;name&quot;: &quot;Ellen&quot;,
  &quot;imgUrl&quot;: &quot;https://www.geo.tv/assets/uploads/updates/2021-03-18/340307_5260100_updates.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f8&quot;,
  &quot;name&quot;: &quot;Bill Gates&quot;,
  &quot;imgUrl&quot;: &quot;https://www.incimages.com/uploaded_files/image/1920x1080/getty_1185999101_20001333200092800_443629.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4f9&quot;,
  &quot;name&quot;: &quot;Taylor Swift&quot;,
  &quot;imgUrl&quot;: &quot;https://static.onecms.io/wp-content/uploads/sites/20/2020/12/02/taylor-swift1.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4fa&quot;,
  &quot;name&quot;: &quot;Engin Altan&quot;,
  &quot;imgUrl&quot;: &quot;https://www.incpak.com/wp-content/uploads/2020/09/50094085_2224186024567959_693900883193935752_n.jpg&quot;,
  &quot;__v&quot;: 0
  },
  {
  &quot;_id&quot;: &quot;605a53881dedb514dcc1c4fb&quot;,
  &quot;name&quot;: &quot;Esra Bilgic&quot;,
  &quot;imgUrl&quot;: &quot;https://i.dawn.com/large/2021/01/6007fbceb61de.png&quot;,
  &quot;__v&quot;: 0
  }
  ]
  </code></pre>
  <p>But when I deployed the same server to heroku, it is showing an empty object when I testing it.</p>
  <p>When I run this command into my terminal:
  <strong>$ heroku logs --tails</strong></p>
  <p>It is showing this error:</p>
  <pre><code>2021-03-25T11:08:08.715303+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: Could not connect to
  any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted.
   Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
  2021-03-25T11:08:08.715323+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:839:32)
  2021-03-25T11:08:08.715324+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:348:10
  2021-03-25T11:08:08.715326+00:00 app[web.1]: at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
  2021-03-25T11:08:08.715326+00:00 app[web.1]: at new Promise (&lt;anonymous&gt;)
  2021-03-25T11:08:08.715327+00:00 app[web.1]: at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
  2021-03-25T11:08:08.715328+00:00 app[web.1]: at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1152:10)
  2021-03-25T11:08:08.715328+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:347:20)
  2021-03-25T11:08:08.715329+00:00 app[web.1]: at Object.&lt;anonymous&gt; (/app/server.js:23:10)
  2021-03-25T11:08:08.715329+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)
  2021-03-25T11:08:08.715329+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
  2021-03-25T11:08:08.715330+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
  2021-03-25T11:08:08.715330+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
  2021-03-25T11:08:08.715331+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
  2021-03-25T11:08:08.715331+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
  2021-03-25T11:08:08.715332+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
  2021-03-25T11:08:08.721117+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated
  either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To term
  inate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html
  #cli_unhandled_rejections_mode). (rejection id: 1)
  2021-03-25T11:08:08.721508+00:00 app[web.1]: (node:21) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the fut
  ure, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  <p>my server that is deployed on heroku is showing empty object instead of that data that I have posted,
  when I type URL of heroku server i.e <strong><a href="https://tinder-clone-backend-mern.herokuapp.com/tinder/cards" rel="nofollow noreferrer">https://tinder-clone-backend-mern.herokuapp.com/tinder/cards</a></strong> :</p>
  <pre><code>{}
  </code></pre>
  <p>How can I make this server work perfectly as the local server does?</p>