<?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: Eli Brown-Henderson</title>
    <description>The latest articles on DEV Community by Eli Brown-Henderson (@cosmiceli).</description>
    <link>https://dev.to/cosmiceli</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%2F636110%2F019c5267-9cea-4e7e-b560-6fe56b1d227d.jpeg</url>
      <title>DEV Community: Eli Brown-Henderson</title>
      <link>https://dev.to/cosmiceli</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cosmiceli"/>
    <language>en</language>
    <item>
      <title>How to set up React Router</title>
      <dc:creator>Eli Brown-Henderson</dc:creator>
      <pubDate>Sun, 06 Jun 2021 21:12:48 +0000</pubDate>
      <link>https://dev.to/cosmiceli/how-to-set-up-react-router-ako</link>
      <guid>https://dev.to/cosmiceli/how-to-set-up-react-router-ako</guid>
      <description>&lt;p&gt;One of the most advantageous parts of using the JavaScript library, React, is the existence of the virtual DOM. The virtual DOM makes Single Page Applications so powerful with its dynamic loading.&lt;/p&gt;

&lt;p&gt;Because React is only concerned with the state of its components, If set up correctly, it only re-renders the components that need to be updated. This makes it possible for dynamic rendering to happen.&lt;/p&gt;

&lt;p&gt;If you want to switch pages in a react application there are ways to do it dynamically, such as conditional rendering of a “Page”. Conditional rendering is useful for small, non-complex applications. For more robust applications that may have a more intricate tree structure, there is a more powerful tool. &lt;/p&gt;

&lt;p&gt;React Router is a library for handling routing in React. It is more powerful than conditional rendering by allowing the developer to use, redirection, parameters, dynamic URL’s, and Props. &lt;/p&gt;

&lt;p&gt;For the sake of this tutorial, we will only go over setting up the basics for a React application to use React Router. &lt;/p&gt;

&lt;p&gt;To begin, You need to create a new React application of course. If You don’t know how to do this, check out my tutorial on setting up a react application &lt;a href="https://dev.to/cosmiceli/setup-a-new-react-application-1f86"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Once you have your application all set up, We are going to set up the application to make it as basic as possible. &lt;/p&gt;

&lt;p&gt;We first will go into the project’s src directory and remove every file inside.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobpqvuvcoiel12bnn6ed.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fobpqvuvcoiel12bnn6ed.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, we are going to make 5 simple files, App.js, index.js, Page1.js, Page2.js, and Page3.js. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnk0bvpc3pwvrv740aqx.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnk0bvpc3pwvrv740aqx.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this is finished, we will set up the index.js to render the App.js function. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawxdwml00rm7s4y8ixdu.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fawxdwml00rm7s4y8ixdu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Next, we will set up the App.js component, and the Page components.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnmkz242929gh291jes5.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjnmkz242929gh291jes5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Page1.js&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8y91m9brrmnqsgd80ykw.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8y91m9brrmnqsgd80ykw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Page2.js&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf4xij2gkb4m6u1cfidz.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf4xij2gkb4m6u1cfidz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Page3.js&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf151d271dmu5g3et03a.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkf151d271dmu5g3et03a.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that this step is complete, we will install the react-router-dom dependency so we can set up React Router. We just need to open the terminal in the directory where our react project is and use the command 'npm install react-router-dom'. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer67il0olxqbmijh4t3v.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer67il0olxqbmijh4t3v.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once we’ve done this, we can start setting up react-router. &lt;br&gt;
In the App component, we need to import some components from react-router.&lt;br&gt;
These components are Browser Router, Switch, and Route&lt;br&gt;
We also need import the Page components, and wrap them in routes.&lt;/p&gt;

&lt;p&gt;With this done, we can now set up our Router. &lt;br&gt;
The router will allow any components inside of it to be routed. &lt;/p&gt;

&lt;p&gt;Inside the routes we need to set the route paths that determine which component will be rendered. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fziedzvt7a98utg60uq5x.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fziedzvt7a98utg60uq5x.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this done, We can now start the application and go to any of the routes that we set up, and the components will be rendered. &lt;br&gt;
the 3 routes are &lt;/p&gt;

&lt;p&gt;localhost:3000&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq9rhmylonsgo8yt6xow.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgq9rhmylonsgo8yt6xow.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
localhost:3000/page2&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6kg4v0smwtumj2id9492.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6kg4v0smwtumj2id9492.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
localhost:3000/page3&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnpamax0xzjahsr2t3cg.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgnpamax0xzjahsr2t3cg.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are essentially finished but we can take it a couple of steps further to really drive it home. &lt;/p&gt;

&lt;p&gt;We can add links to each of the pages so that we can switch pages whenever we need to. All we need for this, is to import Link from react router. &lt;/p&gt;

&lt;p&gt;page 1&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps49hn79xrbbgoevwgtv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fps49hn79xrbbgoevwgtv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;page 2&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8srgvqbyyhwjm3cf9rm.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8srgvqbyyhwjm3cf9rm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;page 3&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgiuyhymamwpxrcyyfwm4.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgiuyhymamwpxrcyyfwm4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have a directory on every page&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5srn0a7o9xnc69va4ar3.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5srn0a7o9xnc69va4ar3.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, If we wanted to have a navigation bar at the top of the page for every page, we don't need to add it to every page component, we only need to add it at the top of the router in the app component. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67637vrnry8dbynhhr2g.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67637vrnry8dbynhhr2g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now We have a 'navbar' on every page!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcmzcly2iw8j2la6b5s3k.JPG" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcmzcly2iw8j2la6b5s3k.JPG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is all we need to get started with using react router. Now you should be able to setup simple routes, and links. You can build on this knowledge to create redirections, histories, directories, navbars, and much more. &lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Setup a new React Application</title>
      <dc:creator>Eli Brown-Henderson</dc:creator>
      <pubDate>Sun, 06 Jun 2021 19:48:00 +0000</pubDate>
      <link>https://dev.to/cosmiceli/setup-a-new-react-application-1f86</link>
      <guid>https://dev.to/cosmiceli/setup-a-new-react-application-1f86</guid>
      <description>&lt;p&gt;React is one of the most popular JavaScript libraries for creating interactive user interfaces.&lt;br&gt;
It makes it easy to integrate your HTML with JavaScript by using JSX. &lt;/p&gt;

&lt;p&gt;It is extraordinarily easy to get started using React with minimal knowledge of web-pack, or babble by using create react app. Setting up quickly allows you to get straight into learning to develop react applications, and exploring the concept of the Virtual DOM without having to worry about setting up a bunch of building tools and dependencies.&lt;/p&gt;

&lt;p&gt;To Get started with using React, you should first make sure that you have an updated version of node on your system. &lt;/p&gt;

&lt;p&gt;You can check your node version by using the command node -v in your terminal.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5xBxqDwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3xfy2wwdzik35u0bfsl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5xBxqDwq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3xfy2wwdzik35u0bfsl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you are sure that you have a properly updated version of node, you can get started with creating your React application. &lt;/p&gt;

&lt;p&gt;Next, you can create a new directory, then open your terminal and cd into that directory. &lt;/p&gt;

&lt;p&gt;After you have entered the directory that you are going to  create your application in, you can enter the command  npx create-react-app demo&lt;/p&gt;

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

&lt;p&gt;This will create a new directory inside of your project directory with the name demo, the demo directory contains your newly created React project. &lt;/p&gt;

&lt;p&gt;It is useful to create the directory inside of the project directory if you would like to set up a back-end, or service directory in your project. &lt;/p&gt;

&lt;p&gt;If you just want to create the React project directly in the directory that you created, you have to make sure that the directory has all lowercase letters and no spaces(Failure to do this will result in an error when trying to create the react project). You then enter the command npx create-react-app.&lt;/p&gt;

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

&lt;p&gt;The ‘.’ allows the project to be created in the current directory. &lt;/p&gt;

&lt;p&gt;All that is left is to start the project. &lt;br&gt;
Cd into the new project with your terminal, and enter the command npm start&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m0q8fEdL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1rayn2jfyuzsfcci7a1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m0q8fEdL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1rayn2jfyuzsfcci7a1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
The react project will start and open up in your current browser. &lt;/p&gt;

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

&lt;p&gt;Now you can get to learning more about developing react applications. &lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Connecting to a local MogoDB</title>
      <dc:creator>Eli Brown-Henderson</dc:creator>
      <pubDate>Mon, 31 May 2021 05:32:40 +0000</pubDate>
      <link>https://dev.to/cosmiceli/connecting-to-a-local-mogodb-3iak</link>
      <guid>https://dev.to/cosmiceli/connecting-to-a-local-mogodb-3iak</guid>
      <description>&lt;p&gt;For this tutorial, I suggest some basic knowledge in using express and NodeJS. &lt;br&gt;
I also suggest having using postman and MongoDB compass. &lt;/p&gt;
&lt;h1&gt;
  
  
  Connecting
&lt;/h1&gt;

&lt;p&gt;When creating a new node CRUD application that uses mongodb, it is important to test your endpoints by using live data. But, when you need your application to be connected quickly to the database, you don't want to spend your time waiting on a new cluster to be deployed. Also, if you are using a paid plan for your projects, and you want to test your application before setting the project up, this will be good for simple data storage. &lt;/p&gt;
&lt;h3&gt;
  
  
  Make sure that MongoDB is installed
&lt;/h3&gt;

&lt;p&gt;The first thing we need to do, is to make sure that MongoDB is installed on our machine. If we enter the command &lt;br&gt;
&lt;code&gt;mongod --version&lt;/code&gt;&lt;br&gt;
we should see the version of MongoDB server we have. &lt;br&gt;
&lt;code&gt;mongo -version&lt;/code&gt;&lt;br&gt;
Will allow you to check the shell version. &lt;/p&gt;

&lt;p&gt;If you get errors when entering these commands in your command line, you may need to configure or install mongoDB on your machine. &lt;/p&gt;
&lt;h4&gt;
  
  
  Using MongoDB compass
&lt;/h4&gt;

&lt;p&gt;This is a MongoDB GUI that allows you to admin your Database projects locally, you could do this using atlas, but using this GUI is great to quickly connect to and admin your clusters. &lt;/p&gt;

&lt;p&gt;You can download it here &lt;a href="https://www.mongodb.com/products/compass"&gt;MongoDV Compass&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Create a new node project
&lt;/h3&gt;

&lt;p&gt;If you already have MongoDB installed, you can proceed to create a new node project. &lt;/p&gt;

&lt;p&gt;In your command line, create a new directory&lt;br&gt;
here we can call it, localConn&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir localconn&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and cd into it &lt;br&gt;
&lt;code&gt;cd localconn&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In that new directory, run the command &lt;br&gt;
&lt;code&gt;npm init -y&lt;/code&gt;&lt;br&gt;
This will allow you create a new node project with the default names. &lt;/p&gt;

&lt;p&gt;We can open the directory in our code editor and make a new file called server.js&lt;/p&gt;
&lt;h3&gt;
  
  
  Install dependencies
&lt;/h3&gt;

&lt;p&gt;In our command line, we need to install the dependencies needed for the database and node server to work &lt;/p&gt;

&lt;p&gt;For this simple demo, we can install express and mongoose&lt;br&gt;
express allows us to host our server,&lt;br&gt;
and mongoose allows us to easily interact with MongoDB&lt;/p&gt;
&lt;h3&gt;
  
  
  Setup the project
&lt;/h3&gt;

&lt;p&gt;We first need to import our dependencies&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express'); 
const mongoose = require('mongoose'); 

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

&lt;/div&gt;



&lt;p&gt;Next, we need to initialize the express server with &lt;br&gt;
&lt;code&gt;const app = express()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then we need a variable with the port number that we want the app to listen on &lt;br&gt;
&lt;code&gt;const port = 3001&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We then need the server to actually listen on the port &lt;/p&gt;

&lt;p&gt;we add at the end of the server.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.listen(port, () =&amp;gt; {
  console.log(`Server started on port ${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Test the server
&lt;/h6&gt;

&lt;p&gt;We can start the server with&lt;br&gt;
&lt;code&gt;npm start&lt;/code&gt;&lt;br&gt;
We will need to restart the server after making changes. &lt;/p&gt;
&lt;h3&gt;
  
  
  Create a model to use
&lt;/h3&gt;

&lt;p&gt;We need some data to add to our Mongo Database, for this demo we can use a simple User object. &lt;/p&gt;

&lt;p&gt;We just need to make a new file called User.js and create the User model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
  name: {
    type: String,
  },
  age: {
    type: Number,
  },
});

module.exports = mongoose.model("User", userSchema);

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

&lt;/div&gt;



&lt;p&gt;Then we need to import the model into the server file. &lt;br&gt;
The module import should now look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const User = require('./User')
const express = require('express')
const mogoose = require('mongoose')
const app = express(); 
const port = 3001; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then need to enable the ability for the server to use JSON objects&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.use(express.json())&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect the to local MongoDB
&lt;/h3&gt;

&lt;p&gt;Now we just have to connect to the DataBase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mongoose
  .connect("mongodb://localhost:27017/demoApp", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() =&amp;gt; {
    console.log("connected via mongodb");
  })
  .catch((err) =&amp;gt; console.log(err));

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

&lt;/div&gt;



&lt;p&gt;Here we are using the &lt;code&gt;.then()&lt;/code&gt; syntax because the connection returns a promise. &lt;/p&gt;

&lt;p&gt;With this, we are able to connect the DB with the name 'demoApp'. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create a route to make a new User doc
&lt;/h2&gt;

&lt;p&gt;Now to make a new user and add it to the database, &lt;br&gt;
we can make a new post route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/user", async (req, res) =&amp;gt; {
  const { name, age } = req.body;
  try {
    const newUser = new User({
      name,
      age,
    });

    newUser.save();
    res.json(newUser);
  } catch (error) {
    res.status(500).json(error);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pulls the name and body fields from the req.body object. &lt;/p&gt;

&lt;p&gt;We then make a new user with the name and age, and save it to the db. &lt;/p&gt;

&lt;p&gt;We can then start the server &lt;br&gt;
&lt;code&gt;npm start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can then actually create the new user, using postman we can send the information&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qhW1-hYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/YgwVrM2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qhW1-hYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/YgwVrM2.jpg" alt="User example" width="880" height="416"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Connect to compass
&lt;/h2&gt;

&lt;p&gt;To check on our Database we can connect to the compass app. We can take the connection string that we used to connect the mongodb and enter it into the compass connection field. &lt;br&gt;
&lt;code&gt;mongodb://localhost:27017/demoApp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4CRf-qPu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GKopw4o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4CRf-qPu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/GKopw4o.jpg" alt="connect to compass" width="627" height="419"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Check the DB
&lt;/h3&gt;

&lt;p&gt;You should see your new DB under the name demoApp, once you click that you should now have a new user collection in the DB. Once you enter that, you can see the new User.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b-neD0d_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zO2dlsg.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b-neD0d_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/zO2dlsg.jpg" alt="new User" width="880" height="307"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;With this you can see that creating and testing a new DB can be quick and easy, with a small bit of setup you can test a full CRUD application. &lt;/p&gt;

&lt;p&gt;Here is what the final code should look like&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const User = require("./User");
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const port = 3001;

app.use(express.json());

mongoose
  .connect("mongodb://localhost:27017/demoApp", {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
  })
  .then(() =&amp;gt; {
    console.log("connected via mongodb");
  })
  .catch((err) =&amp;gt; console.log(err));

app.get("/", (req, res) =&amp;gt; {
  res.send("server running");
});

app.post("/user", async (req, res) =&amp;gt; {
  const { name, age } = req.body;
  try {
    const newUser = new User({
      name,
      age,
    });

    newUser.save();
    res.json(newUser);
  } catch (error) {
    res.status(500).json(error);
  }
});

app.listen(port, () =&amp;gt; {
  console.log(`Server started on port ${port}`);
});

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;User.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongoose = require("mongoose");

const userSchema = mongoose.Schema({
  name: {
    type: String,
  },
  age: {
    type: Number,
  },
});

module.exports = mongoose.model("User", userSchema);

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

&lt;/div&gt;



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