<?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: Hans</title>
    <description>The latest articles on DEV Community by Hans (@codehrafn).</description>
    <link>https://dev.to/codehrafn</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%2F310830%2F3ebebc67-c8cd-4862-9315-26534a959551.jpg</url>
      <title>DEV Community: Hans</title>
      <link>https://dev.to/codehrafn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codehrafn"/>
    <language>en</language>
    <item>
      <title>How to make a simple webcrawler with JAVA ….(and jsoup)</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Mon, 17 May 2021 09:05:30 +0000</pubDate>
      <link>https://dev.to/codehrafn/how-to-make-a-simple-webcrawler-with-java-and-jsoup-434p</link>
      <guid>https://dev.to/codehrafn/how-to-make-a-simple-webcrawler-with-java-and-jsoup-434p</guid>
      <description>&lt;p&gt;While Python is arguably the numero uno language to use when it comes to webscraping, good ole JAVA has it’s perks. At least for a JAVA developer like me who hasn’t quite yet delved in Python. If you are in a hurry, dont’t worry. The complete code is found at the end of this post.&lt;/p&gt;

&lt;p&gt;Anywho, I wanted to figure out how to make a webcrawler w/JAVA, just for the lulz really. Turns out. It was way easier than expected. First of all you need to download jsoup(that is, you need to start a new JAVA project as well)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jsoup.org/download"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now as soon as IntelliJ has done its magic making your project you put the downloaded jsoup .jar file in the project root.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6BCT5la5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3hpun2cvcswkxj72u6b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6BCT5la5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3hpun2cvcswkxj72u6b.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now time to add som nice programming principles right? imports needed for this project are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.ArrayList;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next — its constant’s time. I chose to index links (hrefs) on the CNN website. I chose CNN bc i am not a big fan of FOX news.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public class Crawler {
    public static final String CNN = "https://edition.cnn.com/";
public static void main(String[] args) {
System.out.println("Web Crawler ")
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we need two methods utilising a cluster methods from the jsoup library&lt;br&gt;
First one is a recursive method which indexes said page’s “a href=”´s. To not make the the method ramble on in infinity like a good recursive method will do I chose to make it stop two levels down.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static void crawl (int level, String url, ArrayList&amp;lt;String&amp;gt; visited) {
    if(level &amp;lt;=2 ) {
        Document doc = request(url, visited);
        if (doc!= null) {
            for (Element link : doc.select("a[href]")) {
                 String next_link = link.absUrl("href");
                 if(visited.contains(next_link) == false) {
                     crawl(level++, next_link, visited);
                 }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we need a Document method that checks for connection and returns a string of “Link “ + the url that is indexed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private static Document request(String url, ArrayList&amp;lt;String&amp;gt; v) {
    try {
        Connection con = Jsoup.connect(url);
        Document doc = con.get();
        if(con.response().statusCode() == 200) {
            System.out.println("Link: " + url);
            System.out.println(doc.title());
            v.add(url);
            return doc;
        }
        return null;
    } catch (IOException e) {
        return null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally the full code is ready to be run now&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package.com.company
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class Crawler {
    public static final String CNN = "https://edition.cnn.com/";


    public static void main(String[] args) {
      String url = CNN;
        crawl(1, url, new ArrayList&amp;lt;String&amp;gt;());

    }

    private static void crawl (int level, String url, ArrayList&amp;lt;String&amp;gt; visited) {
        if(level &amp;lt;=2 ) {
            Document doc = request(url, visited);
            if (doc!= null) {
                for (Element link : doc.select("a[href]")) {
                     String next_link = link.absUrl("href");
                     if(visited.contains(next_link) == false) {
                         crawl(level++, next_link, visited);
                     }
                }
            }
        }
    }
    private static Document request(String url, ArrayList&amp;lt;String&amp;gt; v) {
        try {
            Connection con = Jsoup.connect(url);
            Document doc = con.get();
            if(con.response().statusCode() == 200) {
                System.out.println("Link: " + url);
                System.out.println(doc.title());
                v.add(url);
                return doc;
            }
            return null;
        } catch (IOException e) {
            return null;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cheers &lt;br&gt;
&lt;a href="https://boreatech.medium.com/"&gt;https://boreatech.medium.com/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>web</category>
      <category>datascience</category>
    </item>
    <item>
      <title>Workstation 2.0</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Sat, 13 Mar 2021 20:15:14 +0000</pubDate>
      <link>https://dev.to/codehrafn/workstation-2-0-256k</link>
      <guid>https://dev.to/codehrafn/workstation-2-0-256k</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I_bx-OZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yf79c12n23lvob54jhf.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I_bx-OZq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8yf79c12n23lvob54jhf.jpeg" alt="image0"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Difference between react props vs. state</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Sun, 28 Feb 2021 08:45:23 +0000</pubDate>
      <link>https://dev.to/codehrafn/react-props-vs-state-56l5</link>
      <guid>https://dev.to/codehrafn/react-props-vs-state-56l5</guid>
      <description>&lt;p&gt;One of the core concepts of react is the difference between props and state. Only changes in props and state trigger react to re-render components and update the DOM.&lt;/p&gt;

&lt;p&gt;The biggest difference is that re-rendering of the component based on input in state is done entirely within the component, whereas you with using props can receive new data from outside the component and re-render.&lt;/p&gt;

&lt;h3&gt;
  
  
  Props
&lt;/h3&gt;

&lt;p&gt;props allows you to pass data from a parent component to a child component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Parent Component&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt; &lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Data structures and algorithms with JAVA&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;   &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;//Child component&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation: Now. ‘props’ is passed in to the child component and the functional component the passes the ‘props’ as an argument which in turn will be handled as an object. The property ‘title’ is accessible in the child component from the parent component.&lt;/p&gt;

&lt;h3&gt;
  
  
  State
&lt;/h3&gt;

&lt;p&gt;Only class-based react components can define and use state. It’s although possible to pass state to a functional component but functional components can’t edit them directly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;NewBook&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; 
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the NewBook component contains a defined state. This state is accessible through this.state.number and can be returned in the render() method.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
      <category>react</category>
      <category>programming</category>
    </item>
    <item>
      <title>Connect to postgresdatabase with JAVA</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Thu, 16 Jan 2020 06:48:07 +0000</pubDate>
      <link>https://dev.to/codehrafn/connect-to-postgresdatabase-with-java-4d7a</link>
      <guid>https://dev.to/codehrafn/connect-to-postgresdatabase-with-java-4d7a</guid>
      <description>&lt;p&gt;Feel free to put variables and dbConnection() in another class.&lt;/p&gt;

&lt;p&gt;import java.sql.Connection;&lt;br&gt;
import java.sql.DriverManager;&lt;br&gt;
import java.sql.SQLException;&lt;br&gt;
import java.util.logging.Level;&lt;br&gt;
import java.util.logging.Logger;&lt;br&gt;
import javax.swing.JOptionPane;&lt;/p&gt;

&lt;p&gt;/*&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Need library Postgresql -&amp;gt; installed in library &lt;/li&gt;
&lt;li&gt;Simple connection to postgres&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.tutorialspoint.com/postgresql/postgresql_java.htm"&gt;https://www.tutorialspoint.com/postgresql/postgresql_java.htm&lt;/a&gt;
*/&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;public class DatabaseConnection {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Connection connection;
String url = "jdbc:postgresql://localhost:5432/databasename";
String user = "username";
String password = "yourpassword";

public static void main(String[] args) throws ClassNotFoundException {
    DatabaseConnection db = new DatabaseConnection();
    db.dbConnection();
}

public Connection dbConnection() {
    try {
        Class.forName("org.postgresql.Driver");
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        DriverManager.getConnection(url, user, password);
        JOptionPane.showMessageDialog(null, "connected");

    } catch (SQLException ex) {
        Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
        JOptionPane.showMessageDialog(null, "Failed to connect");
    }
    return connection;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

</description>
      <category>java</category>
      <category>postgres</category>
      <category>oop</category>
      <category>sql</category>
    </item>
    <item>
      <title>Connect your node backend to postgresql database</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Tue, 14 Jan 2020 20:22:39 +0000</pubDate>
      <link>https://dev.to/codehrafn/connect-your-node-backend-to-postgresql-database-4i6b</link>
      <guid>https://dev.to/codehrafn/connect-your-node-backend-to-postgresql-database-4i6b</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;npm init -&amp;gt; entry point: server.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create file "server.js" in project root&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;npm install express pg nodemon&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in package.json add script "start":"nodemon"&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;{&lt;br&gt;
  "name": "server",&lt;br&gt;
  "version": "1.0.0",&lt;br&gt;
  "description": "",&lt;br&gt;
  "main": "server.js",&lt;br&gt;
  "scripts": {&lt;br&gt;
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",&lt;br&gt;
    "start":"nodemon"&lt;br&gt;
  },&lt;br&gt;
  "author": "",&lt;br&gt;
  "license": "ISC",&lt;br&gt;
  "dependencies": {&lt;br&gt;
    "express": "^4.17.1",&lt;br&gt;
    "nodemon": "^2.0.2",&lt;br&gt;
    "pg": "^7.17.1"&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;in project root -&amp;gt; make folder "config"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;make folder db.js and add the following&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;const { Pool, Client } = require("pg");&lt;/p&gt;

&lt;p&gt;const pool = new Pool({&lt;br&gt;
  user: 'postgres',&lt;br&gt;
  host: 'localhost',&lt;br&gt;
  database: '',&lt;br&gt;
  password: '',&lt;br&gt;
  port: 5432&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;pool.connect(err =&amp;gt; {&lt;br&gt;
    if (err) {&lt;br&gt;
      console.error('connection error', err.stack)&lt;br&gt;
    } else {&lt;br&gt;
      console.log('connected')&lt;br&gt;
    }&lt;br&gt;
  });&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;in file server.js add following&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;const express = require('express');&lt;br&gt;
const app = express();&lt;br&gt;
const pool = require ('./config/db');&lt;/p&gt;

&lt;p&gt;const PORT = process.env.PORT || 5000;&lt;br&gt;
app.listen(PORT, () =&amp;gt; {&lt;br&gt;
console.log(&lt;code&gt;Listening to port: ${PORT}&lt;/code&gt;);&lt;br&gt;
});&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;npm start &lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>postgres</category>
      <category>sql</category>
    </item>
    <item>
      <title>Make aliases in ubuntu</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Tue, 14 Jan 2020 12:12:43 +0000</pubDate>
      <link>https://dev.to/codehrafn/make-aliases-in-ubuntu-886</link>
      <guid>https://dev.to/codehrafn/make-aliases-in-ubuntu-886</guid>
      <description>&lt;p&gt;Easiest way to make aliases of often used commands is to open file manager and then hit ctrl-h to show the hidden files. Open the .bashrc file and scroll down to the bottom of the file&lt;/p&gt;

&lt;p&gt;The syntax is: &lt;em&gt;alias='commands'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For example you can combine these two commands with a simple alias&lt;/p&gt;

&lt;p&gt;&lt;em&gt;alias ago='sudo apt-get update &amp;amp;&amp;amp; sudo apt-get upgrade'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now save the file, open a terminal window and hit 'ago'&lt;/p&gt;

&lt;p&gt;I like to hit 'ls' everytime I cd into a folder just to get a view of the newly entered folder so therefore i like to combine the cd command with a ls. However, this kind of command needs a function as opposed to an alias&lt;/p&gt;

&lt;p&gt;Type:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;function cd {&lt;br&gt;
builtin cd "$@" &amp;amp;&amp;amp; ls&lt;br&gt;
 }&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;hit save and open up a new terminal window and test it&lt;/p&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>Connect your node app to MongoDB in ten easy steps</title>
      <dc:creator>Hans</dc:creator>
      <pubDate>Tue, 14 Jan 2020 12:06:49 +0000</pubDate>
      <link>https://dev.to/codehrafn/connect-your-node-app-to-mongodb-in-ten-easy-steps-4ijd</link>
      <guid>https://dev.to/codehrafn/connect-your-node-app-to-mongodb-in-ten-easy-steps-4ijd</guid>
      <description>&lt;ol&gt;
&lt;li&gt;&lt;p&gt;npm init -&amp;gt; entry point: server.js&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;create file "server.js" in project root&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;npm install mongoose express config nodemon&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in package.json add script "start":"nodemon"&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;{&lt;br&gt;
  "name": "server",&lt;br&gt;
  "version": "1.0.0",&lt;br&gt;
  "description": "",&lt;br&gt;
  "main": "server.js",&lt;br&gt;
  "scripts": {&lt;br&gt;
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",&lt;br&gt;
    "start":"nodemon"&lt;br&gt;
  },&lt;br&gt;
  "author": "",&lt;br&gt;
  "license": "ISC",&lt;br&gt;
  "dependencies": {&lt;br&gt;
    "config": "^3.2.4",&lt;br&gt;
    "express": "^4.17.1",&lt;br&gt;
    "mongoose": "^5.8.7",&lt;br&gt;
    "nodemon": "^2.0.2"&lt;br&gt;
  }&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;in project root -&amp;gt; make folder "config"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in folder "config" make two files -&amp;gt; "db.js" and "default.json"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;in file "default.json" add the link with your mongodb credentials&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;{&lt;br&gt;
    "MongoURI":"mongodb+srv://username:&lt;a href="mailto:yourpassword@aquacontrol-atxjt.mongodb.net"&gt;yourpassword@aquacontrol-atxjt.mongodb.net&lt;/a&gt;/test?retryWrites=true&amp;amp;w=majority"&lt;br&gt;
}&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;in file "db.js" - add the following:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;const mongoose = require('mongoose');&lt;br&gt;
const config = require('config');&lt;br&gt;
const database = config.get("MongoURI");&lt;/p&gt;

&lt;p&gt;const connectDB = async () =&amp;gt; {&lt;br&gt;
    try {&lt;br&gt;
        await mongoose.connect(database, {&lt;br&gt;
            useNewUrlParser: true,&lt;br&gt;
            useUnifiedTopology: true&lt;br&gt;
        });&lt;br&gt;
        console.log('Connected to mongoDB');&lt;br&gt;
    } catch (err) {&lt;br&gt;
        console.error(err.message);&lt;br&gt;
        process.exit(1);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;module.exports = connectDB;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;in "server.js" - add the following:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;const express = require('express');&lt;br&gt;
const app = express();&lt;/p&gt;

&lt;p&gt;const PORT = process.env.PORT || 5000;&lt;br&gt;
app.listen(PORT, () =&amp;gt; console.log(&lt;code&gt;Server started on port ${PORT}&lt;/code&gt;));&lt;br&gt;
/*&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connecting to mongoDB 
*/
const connectDB = require('./config/database');
connectDB();
app.use(express.json({
extended: false
}));&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;module.exports = connectDB;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;npm start&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;B&lt;/p&gt;

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