<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: cpclark360</title>
    <description>The latest articles on DEV Community by cpclark360 (@cpclark360).</description>
    <link>https://dev.to/cpclark360</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F172092%2F414e941d-c042-467a-9c7b-796a707e73f6.png</url>
      <title>DEV Community: cpclark360</title>
      <link>https://dev.to/cpclark360</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cpclark360"/>
    <language>en</language>
    <item>
      <title>How to host a RESTful Node.js server with MongoDB Atlas database on Heroku</title>
      <dc:creator>cpclark360</dc:creator>
      <pubDate>Sun, 24 Nov 2019 19:51:42 +0000</pubDate>
      <link>https://dev.to/cpclark360/how-to-host-a-restful-node-js-server-with-mongodb-atlas-database-on-heroku-1opl</link>
      <guid>https://dev.to/cpclark360/how-to-host-a-restful-node-js-server-with-mongodb-atlas-database-on-heroku-1opl</guid>
      <description>&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Node.js - Installed&lt;/li&gt;
&lt;li&gt;MongoDB Atlas  - AWS free tier cluster Account&lt;/li&gt;
&lt;li&gt;Text-editor - Visual Studio Code, Atom and etc.&lt;/li&gt;
&lt;li&gt;Git - Installed&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Dependencies Used
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;cors       version 2.8.5&lt;/li&gt;
&lt;li&gt;express    version 4.17.1&lt;/li&gt;
&lt;li&gt;mongoose   version 5.7.12&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Assumptions
&lt;/h3&gt;

&lt;p&gt;The RESTful Node.js server with MongoDB database works when running on your localhost.&lt;/p&gt;

&lt;h5&gt;
  
  
  The MongoDB Connection String, you can find it by:
&lt;/h5&gt;

&lt;ol&gt;
&lt;li&gt;Sign-in at mongoDB Altas &lt;/li&gt;
&lt;li&gt;Locate the cluster you're using for server&lt;/li&gt;
&lt;li&gt;Click " CONNECT" &amp;gt; Click " Connect Your Application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FQdFnS3d1%2Fmongodb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FQdFnS3d1%2Fmongodb.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Set MongoDB Altas - whitelist to &lt;strong&gt;0.0.0.0/0&lt;/strong&gt;
&lt;/h5&gt;

&lt;p&gt;To set click 'Network Access' under Security tab &amp;gt; click 'ADD IP ADDRESS'&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVNYhBGKM%2FScreenshot-from-2019-11-24-09-54-58.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVNYhBGKM%2FScreenshot-from-2019-11-24-09-54-58.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  note
&lt;/h6&gt;

&lt;p&gt;&lt;strong&gt;Whitelist&lt;/strong&gt; - MongoDB Atlas allows client connections to the cluster from entries in the project’s whitelist. You must add an entry to the whitelist to connect the client.CIDR notation 0.0.0.0/0 is used to allow all client connections to the cluster. However, you can add a specific IP address/es.&lt;/p&gt;




&lt;ul&gt;
&lt;li&gt;Go to Heroku dashboard &amp;gt; Select your server App &amp;gt; Settings &amp;gt; Reveal Config Vars&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FrsmTsYLq%2FScreenshot-from-2019-11-24-08-06-33.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FrsmTsYLq%2FScreenshot-from-2019-11-24-08-06-33.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Add ' MONGODB_URL ' as a Config Vars KEY in HEROKU, and add connection string as the VALUE then click 'Add'.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Store  Cluster - connection string as a node.js process environment variable called ' MONGODB_URL ' in the main server javascript file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  For Example: server.js
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = process.env.MONGODB_URL;

const connectDB = async () =&amp;gt; {
  try {
    await mongoose.connect(db, {
      useUnifiedTopology: true,
      useNewUrlParser: true
    });
    console.log("MongoDB is Connected...");
  } catch (err) {
    console.error(err.message);
    process.exit(1);
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;To allow inbound requests to the server using CORS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add FrontEnd site URL where it says "Your FrontEnd Website URL"&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  For Example:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// cors origin URL - Allow inbound traffic from origin
corsOptions = {
  origin: "Your FrontEnd Website URL",
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};
app.use(cors(corsOptions));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h6&gt;
  
  
  note
&lt;/h6&gt;

&lt;p&gt;The code example above is for allowing a single-origin if you want to allow more than one origin refer &lt;a href="https://www.npmjs.com/package/cors#installation" rel="noopener noreferrer"&gt;here&lt;/a&gt; &lt;/p&gt;




&lt;p&gt;To Deploy select your App then click "Deploy" and choose one of the deployment methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2F1XFfGn1j%2FScreenshot-from-2019-11-24-12-32-15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2F1XFfGn1j%2FScreenshot-from-2019-11-24-12-32-15.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Troubleshooting Tips&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you encounter Heroku runtime errors you can view logs below&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVsnThfg9%2FScreenshot-from-2019-11-24-12-13-07.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2FVsnThfg9%2FScreenshot-from-2019-11-24-12-13-07.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  note
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;If you are getting errors stating Heroku can't find your modules when trying to deploy, it may be because you are using relative paths for example: &lt;code&gt;const connectDB = require("./config/db");&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will have to use absolute paths because Heroku creates its own FileSystem at deployment.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>mongodb</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
