<?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: Humberto David Esquerra</title>
    <description>The latest articles on DEV Community by Humberto David Esquerra (@davidfree2).</description>
    <link>https://dev.to/davidfree2</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%2F718973%2Fd03702d7-c069-4ba8-897b-3f7f7dfba8f5.jpeg</url>
      <title>DEV Community: Humberto David Esquerra</title>
      <link>https://dev.to/davidfree2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/davidfree2"/>
    <language>en</language>
    <item>
      <title>I Created a Python Script To Control my ROKU TV</title>
      <dc:creator>Humberto David Esquerra</dc:creator>
      <pubDate>Mon, 30 Jan 2023 08:31:36 +0000</pubDate>
      <link>https://dev.to/davidfree2/i-created-a-python-script-to-control-my-roku-tv-2n40</link>
      <guid>https://dev.to/davidfree2/i-created-a-python-script-to-control-my-roku-tv-2n40</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;For those that don’t know what a Roku Tv is, it’s a Tv that comes with software that can play different popular streaming services like Netflix, Hulu, Disney+, services like that. You can also download an App onto your phone that can control the volume, channels, video outputs, and movement on screen. Although I did find the app very useful, I would often find myself taking my hands off my laptop to use the app on my phone to interact with it. So, like a software developer would, I created a CLI (command line interface) that can interreact with the TV. That way it would be easier for me use the device. In this article I really want to talk about the process I took to go from knowing nothing about the product, to being able to manipulate it through the command line.&lt;/p&gt;

&lt;p&gt;Oh, and also, I will only be talking about how I discovered how to work with the ROKU device and the logical parts that went into the script, the GUI and CLI part will be saved for a different article.&lt;/p&gt;

&lt;h2&gt;
  
  
  First, I had to figure out how it works.
&lt;/h2&gt;

&lt;p&gt;At first, I had no idea how the Roku platform worked, So I investigated with a simple google search for things like, how does Roku work? how does Roku interact with the app? Is there a Roku developer page? After a few searches I finally found this page&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.roku.com/docs/developer-program/debugging/external-control-api.md" rel="noopener noreferrer"&gt;https://developer.roku.com/docs/developer-program/debugging/external-control-api.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is one the developer pages talking about ECP which is their protocol for allowing a device to be controlled over a LAN network. Upon further investigation I discovered that these devices can be controlled be making an empty POST request to the correct socket (socket meaning IP address with port number) with a specific URL route. For example, let’s say your devices socket is 199.111.255.9:1234 to make the device increase its volume you would make a post request to that socket with the URL ending VolumeUp. Here’s an example of me making a Roku device increase its volume, navigate down, and another one navigation home. I will be using the Linux command curl to achieve this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl -d '' "http://199.111.255.9:1234/keypress/VolumeUp"
curl -d '' "http://199.111.255.9:1234/keypress/Down"
curl -d '' "http://199.111.255.9:1234/keypress/Home"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see using curl with the -d flag it’s really easy to control a Roku device. Also notice that the post request has nothing in the body.&lt;/p&gt;

&lt;h2&gt;
  
  
  It works now time to build the CLI!
&lt;/h2&gt;

&lt;p&gt;Now that we understand how to control our ROKU Tv over our network it’s time to make it works in a python script. That way I don’t have to constantly use the curl command alone to work with the TV.&lt;/p&gt;

&lt;p&gt;First, I wanted to come up with the logic to get everything working. I wanted to create a proof of concept, so I started by creating the commands and making sure they work. I decided to create methods in a python class and later binding that class to a variable for easy access. I also wanted to use try, except statements for easy troubleshooting. All in all, I ended up coming up with this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F1mtjn0wy19ge9nr3tu0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F1mtjn0wy19ge9nr3tu0b.png" alt="Image description" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I needed to import the OS command as CURL relies on the Linux Operating System. I then initialized the class to a variable, used the method, and it worked! From here the entire process became simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simple! Now I’ll add more commands and class attributes.
&lt;/h2&gt;

&lt;p&gt;Now that we’ve proven our proof of concept everything became simple ill just add more commands for what I want the ROKU device to do like movement and volume. I also wanted to add properties to the class that would reference the IP address of the device and the port as well. All in all, that looked like this. Nice and neat I’d say.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fyv3zbrricjlgh9h6bv25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fyv3zbrricjlgh9h6bv25.png" alt="Image description" width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Time to wrap everything up.
&lt;/h2&gt;

&lt;p&gt;Now that most of the logic is complete its time to bind the class to a variable and make it run. First, I created a variable called my_device and assigned it to the class, while giving the arguments for the Ip address and port number 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;my_device = Roku ('112.111.647.9', '1234')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like I said I wanted to just go over the discovery of working with the technology and how I got it all to work with python. If you’re interested in seeing the entire source code, with GUIand CLI I’ve attached the link down below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading my article!
&lt;/h2&gt;

&lt;p&gt;The source code to this project contains an actual CLI and a GUI that I’ve created. I just wanted to go over the barebones project and the brunt of the logic here.&lt;/p&gt;

&lt;p&gt;Also, you can check it out here at Davidfree2/roku_tv&lt;/p&gt;

&lt;p&gt;And as always thanks for reading! I hope you found this article enjoyable and learned something in the process. If there’s something that you feel like I’ve left out then comment down below, or message me directly. Also leave a follow if you found this article informative! :)&lt;/p&gt;

&lt;h2&gt;
  
  
  What I’m up to lately.
&lt;/h2&gt;

&lt;p&gt;My names David and I’ve been working on software web development for about 2 years now. Mostly free lance work. I know Python, JavaScript, Bash, CSS, HTML, PostgreSQL, MongoDB, and many other technologies. If you want to get in touch check me out here at davidesquerra.com. At this website you can text me directly or email me. And like I said above thanks for reading :).&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>architecture</category>
      <category>python</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Warning! Are you making this Linux mistakes?</title>
      <dc:creator>Humberto David Esquerra</dc:creator>
      <pubDate>Tue, 18 Oct 2022 05:15:56 +0000</pubDate>
      <link>https://dev.to/davidfree2/warning-are-you-making-this-linux-mistakes-5apl</link>
      <guid>https://dev.to/davidfree2/warning-are-you-making-this-linux-mistakes-5apl</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;Linux is a hard environment to journey into especially if you come from the “normal” world of a Windows OS environment. If your worried to venture into Linux, are new to Linux, Or seem to still have trouble using it then this article is for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn the command line (like as if your life was on the line)
&lt;/h2&gt;

&lt;p&gt;Can you get away without using the Linux shell commands line and just using the GIU? Yes, Should you? NO! The Linux terminal is a powerful tool at your disposal that you can do anything with. Want to move every file from one directory to another? Just use a simple command to move it over. Want to delete every file from a directory? Just use a simple command to delete everything. Want to make a file executable and make it callable from anywhere? Just use a simple command in terminal. There are hundreds (if not thousands) of commands that are built into most Linux flavors. You just have to learn them. But which commands should I learn? Start with navigation. Learn how to move around all the directories. For example commands like ls, cd, pwd. Then you can work your way up to moving, removing, copying, editing files. These commands for example would be rm, cp, mv, nano, and cat. Then after learning the basics you can move your way up to more advanced commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caution! Commands can get risky
&lt;/h2&gt;

&lt;p&gt;Linux is awesome, powerful, and can get you in serious amounts of trouble. Linux in general will give you the power to do what you want. Want to download something totally random off the internet without any research and make it executable? No problem. Want to remove another users entire directory? You can do that. Want to recursively remove half the files on the system in little to no time? Easy peazzy. In general you should be cautious with the rm, sudo, and mv commands. These commands are capable of either removing software, moving software, or gives you super user privileges immediately. If you don’t have to have to sudo something then you probably don’t have to. While moving or removing software can corrupt the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux should work with you, Not against you
&lt;/h2&gt;

&lt;p&gt;There are a lot of different Linux distributions out there, some will work better for YOU than others. If your new to linux you should start with a friendlier more GUI friendly linux flavor. These linux distros would be distros like Ubuntu, Linux Mint, Pop Os, and Elementary Os. Most of these come “right out of the box” with video players, image viewers, rich GUI’s, web browsers, and even office tools. No extra installation needed.Then there are more advanced, bare bones, minimalist and build yourself linux distros. These would include Arch Linux and Gentoo. These types of linux distros expect more expertise and research to make work because you’ll be configure things to your hardware from scratch. If you are a beginner or are someone who wants less hassle to make things work then stick to the more beginner user friendly favors of linux.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading!
&lt;/h2&gt;

&lt;p&gt;This is a mini-article I made awhile ago and I hope you enjoyed it! Wondering what i’m up to lately? Check me out my personal website here at &lt;a href="http://davidesquerra.com"&gt;http://davidesquerra.com&lt;/a&gt; Or here’s my git hub &lt;a href="https://github.com/Davidfree2"&gt;https://github.com/Davidfree2&lt;/a&gt;&lt;/p&gt;

</description>
      <category>linux</category>
      <category>bash</category>
      <category>ubuntu</category>
      <category>programming</category>
    </item>
    <item>
      <title>Bash is powerful! Lets build a simple number guesser game.</title>
      <dc:creator>Humberto David Esquerra</dc:creator>
      <pubDate>Sat, 08 Oct 2022 20:41:34 +0000</pubDate>
      <link>https://dev.to/davidfree2/bash-is-powerful-lets-build-a-simple-number-guesser-game-30d7</link>
      <guid>https://dev.to/davidfree2/bash-is-powerful-lets-build-a-simple-number-guesser-game-30d7</guid>
      <description>&lt;h2&gt;
  
  
  Why Bash? and Why a number guesser?
&lt;/h2&gt;

&lt;p&gt;A number guesser is a less complex project to build and you’ll learn how to work with if, else if, else statements, functions, user input, and the random module (Of course these programming concepts will be done the Bash way!). Wait but what is Bash? Bash is the default login shell for most linux systems. Meaning that to get good at linux it helps to get good at Bash. Bash can be ran straight from the linux terminal and can also be added to a text file and made executable using the linux command line on the spot! Also most (if not all) linux distros support bash so no need to download anything new. Just run it on the fly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alright lets build our game! Here’s the plan!
&lt;/h2&gt;

&lt;p&gt;First we need a way to generate a random number (we’ll use the built in random function) and assign it to a variable that way we can reference the variable and compare it to the user input. Next we need to get user input. Then finally we need to compare the user input to the random number. If the user is guessed right then well end the game and let the user know they won! Else we need to continue the game until the user wins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Alright simple enough, Lets start writing the code!
&lt;/h2&gt;

&lt;p&gt;First we append the shebang (which is short for sharp and bang) and follow that with our /bin/bash path. We do this to ensure that our linux operating system executes the bash file properly. It should 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;#!/bin/bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need a way to read user input through the command line. For this well use the built in read command. Well append that with -p to assign it to a variable (that way we can reference it later). This should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"What number would you like to pick?"&lt;/span&gt; user_guess
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So read -p (read command), “what number would you like to pick” (the printed string), then user_guess (the variable name with assigned value).&lt;/p&gt;

&lt;p&gt;Next we need to compare the user input to the random number to determine whether or not the game player is right. For this well use if/else statements. If/else statements check if a condition is true and will execute a code block depending on condition. In bash this logical statements looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;conditional] &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;do &lt;/span&gt;something here&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;do &lt;/span&gt;something&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also want to print things to the terminal to let the user know whether or not hes correct. For this well use echo. Echo (actually echo lowercase) is another built in command and is used to print strings to the terminal. Echo looks like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"this string will be printed to the terminal screen"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So combining echo and our if statement, our actual code should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;random_number&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;user_guess&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;then
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You guessed right!"&lt;/span&gt;
&lt;span class="k"&gt;else 
    &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You guessed wrong"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Awesome! Lets build the final product!
&lt;/h2&gt;

&lt;p&gt;Now that we have the bare bones logic in order, from here i’ll clean up the code a little and make it more readable. I’ll also add some logic to a function and call that function because (in my opinion) it makes everything more readable, But more importantly I will be using recursion to recall the function if the current user guesses wrong. So here’s the finished product!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Random number guesser!"&lt;/span&gt;                                                                       

&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"What is the random number you would like to guess? Please    
         pick a number between 0 and 9"&lt;/span&gt; user_guess

&lt;span class="k"&gt;function &lt;/span&gt;number_guess&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;                                                                            
   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;random_number&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;user_guess&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;                                                        
   &lt;span class="k"&gt;then                                                                                            
       &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You guessed right! Congratulations &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;random_number&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is 
             equal to &lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;user_guess&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;        
   &lt;span class="k"&gt;else                                                                                            
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You guessed wrong! Try again."&lt;/span&gt;                                                        
        &lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s2"&gt;"What is the random number you would like to guess? 
                please pick a number between 0 and 9"&lt;/span&gt; user_guess
        number_guess                                                                                
    &lt;span class="k"&gt;fi&lt;/span&gt;                                                                                              
&lt;span class="o"&gt;}&lt;/span&gt;                                                                                                   

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

&lt;/div&gt;



&lt;p&gt;As you can see building a simple number guessing game isn’t super difficult. It just takes a few lines of code and because we utilized bash this script is super easy to run from the command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading my article!
&lt;/h2&gt;

&lt;p&gt;Wondering what i’m up to? You can check me out here at &lt;a href="http://davidesquerra.com/"&gt;http://davidesquerra.com/&lt;/a&gt; or here at &lt;a href="https://github.com/Davidfree2"&gt;https://github.com/Davidfree2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you really enjoyed this article drop a clap down below or even a follow! Let me know what you think and if you want to see more content like this. Drop a question too if there’s something that’s not clear enough. Thank you for reading.&lt;/p&gt;

</description>
      <category>bash</category>
      <category>programming</category>
      <category>linux</category>
      <category>ubuntu</category>
    </item>
    <item>
      <title>NodeJS handling POST requests.</title>
      <dc:creator>Humberto David Esquerra</dc:creator>
      <pubDate>Thu, 07 Jul 2022 02:30:31 +0000</pubDate>
      <link>https://dev.to/davidfree2/nodejs-handling-post-requests-131c</link>
      <guid>https://dev.to/davidfree2/nodejs-handling-post-requests-131c</guid>
      <description>&lt;h2&gt;
  
  
  Preface
&lt;/h2&gt;

&lt;p&gt;In the last article I talked about the client server model and how data can be sent from client to server and back to client with the use of GET requests. Although GET requests power the internet (you made a get request just to view this article), Sometimes a client might want to send extra information to the server (you’ve done this for usernames and passwords or adding something to a database). For example in an html form or URL query. We’ll explore this through the use of POST requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  For the reader
&lt;/h2&gt;

&lt;p&gt;In this article I want to delve more into how a client can send more data to a server. This is called a POST request. A POST request is where extra data can be sent to a server where it can be parsed and used by the backend. I will be using NodeJS (other programming languages also offer the use of http requests, and http body parsing with post requests). This article assumes that you know nothing about POST requests and you can follow along step by step through this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do POST requests work?
&lt;/h2&gt;

&lt;p&gt;A POST request is a request method that is supported by HTTP protocol. This type of request encloses data in the HTTP request body. In contrast a GET request does not enclose data in the HTTP request body, and is primarily used to request a certain resource from a server (for example getting a page for Wikipedia). POST requests are often used in conjunction with databases to store data into a database, authentication to authenticate a username and password, or any other task where there has to be more data than just a URL path.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets start by making sure NPM is installed.
&lt;/h2&gt;

&lt;p&gt;First make sure that NodeJS is installed on your machine (this can be different for Linux, mac, and window machines. See NodeJS documentation for more). To do this lets check with the command node -v&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zpIz-iZx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o054xs6gevtojolf2q4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zpIz-iZx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o054xs6gevtojolf2q4.png" alt="Image description" width="875" height="17"&gt;&lt;/a&gt;&lt;br&gt;
You should receive your current NodeJS version as an output (for example I'm currently using version 8.11.0). If you do not receive an output like this refer to the NPM documentation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now lets create our directory and install the required modules.
&lt;/h2&gt;

&lt;p&gt;Now lets create our directory to that’ll hold all our files and dependencies.&lt;/p&gt;

&lt;p&gt;Next use npm install express body-parser . Express is our backend framework that handles HTTP requests (this was covered in the last article) and body-parser is an NPM module that handles POST request data.&lt;/p&gt;

&lt;p&gt;Your terminal should look something like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nQb0mkMN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsi92xxplunvriltdxny.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nQb0mkMN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsi92xxplunvriltdxny.png" alt="Image description" width="875" height="296"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets create the HTML page with the form that we’ll be making a POST request with.
&lt;/h2&gt;

&lt;p&gt;For this I will create another file called index.html (in the real world you probably would have this file in a static, routes, or pages directory I'm just adding it into the root directory for this example.). Also the text in the HTML file should look like this and should include our input fields, and our submit button.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E5usetUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eej33fzmg7uhufmusppy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E5usetUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eej33fzmg7uhufmusppy.png" alt="Image description" width="875" height="210"&gt;&lt;/a&gt;&lt;br&gt;
And your root directory should now look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hq3qWg_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhf5msujo09zofqys8rg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hq3qWg_C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhf5msujo09zofqys8rg.png" alt="Image description" width="875" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now lets move onto the backend we’ll start with handling the homepage GET request.
&lt;/h2&gt;

&lt;p&gt;Finally now we can create the logic that takes in the POST request data, but first we need to handle serving our homepage. For this lets create a server.js file in the root directory. This file will house our backend logic for the form and return something, along with a get request for our homepage and path parameters.&lt;/p&gt;

&lt;p&gt;Lets create a server.js file (commonly called an app.js file) and have it return a homepage, then we can add more code to it and have it work with post requests. The server.js file should look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G7COjotr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p6paqv6qpjx2oci2m10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G7COjotr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9p6paqv6qpjx2oci2m10.png" alt="Image description" width="875" height="144"&gt;&lt;/a&gt;&lt;br&gt;
So far our server.js file (our express file) can handle a GET request to a / URL parameter, returning our homepage. We can check this by running node server.js from the terminal and then going to our web browser (in my case I'm using google chrome) and going to localhost:8080 you should see the HTML file you created earlier and it should look like this.&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GRkVRi34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7lxz9mtzixkiimedni1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GRkVRi34--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7lxz9mtzixkiimedni1.png" alt="Image description" width="875" height="120"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now lets add the logic for our POST request.
&lt;/h2&gt;

&lt;p&gt;Now lets add our POST request logic to our server.js file. Remember that body-parser module we downloaded using NPM? Well that's what we’ll be using here to handle our POST request form data.&lt;/p&gt;

&lt;p&gt;First you’ll import the body-parser module (like how we did with express), then make sure out body-parser variable uses json to handle data. Then add another app method to handle everything. All in all it should look like this. Also make sure to read the picture here to see a more descriptive example of how everything works.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--u-U2_ggX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syjyvpb6gatxjddzoqtu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--u-U2_ggX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/syjyvpb6gatxjddzoqtu.png" alt="Image description" width="875" height="308"&gt;&lt;/a&gt;&lt;br&gt;
Now run the server.js file using node server.js and type into the index.html (at localhost:8080) form. your data and your server.js should receive the incoming request and work with it! All in all it should look like this. Notice that our server.js file has console.log’d our incoming data!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oq3AXD8y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf8ilbhrtca06wim065l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oq3AXD8y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jf8ilbhrtca06wim065l.png" alt="Image description" width="875" height="60"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  You’ve officially handled POST request data!
&lt;/h2&gt;

&lt;p&gt;You did it! You’ve handled a POST request using Express and body-parser! Now you can accept form data from any HTML form, and do something with it. Like adding something to a database, doing something with the data, authentication, or letting the user know that there form was sent! Also you can explore things like Fetch, or Axios to get even more of a handle on POST requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading!
&lt;/h2&gt;

&lt;p&gt;Thanks for reading. I hope you found this article enjoyable and learned something in the process. If there’s something that you feel like I’ve left out then comment down below, or message me directly. Also leave a follow if you found this article informative! :)&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'm up to lately.
&lt;/h2&gt;

&lt;p&gt;My names David and I’ve been working on software web development for about 2 years now. Mostly free lance work. I know Python, JavaScript, Bash, CSS, HTML, PostgreSQL, MongoDB, and many other technologies. If you want to get in touch check me out here at davidesquerra.com. At this website you can text me directly or email me. And like I said above thanks for reading :).&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>api</category>
    </item>
    <item>
      <title>Creating a simple web server using NodeJS</title>
      <dc:creator>Humberto David Esquerra</dc:creator>
      <pubDate>Tue, 14 Jun 2022 03:21:19 +0000</pubDate>
      <link>https://dev.to/davidfree2/creating-a-simple-web-server-using-nodejs-17k7</link>
      <guid>https://dev.to/davidfree2/creating-a-simple-web-server-using-nodejs-17k7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YgBTLfFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9nxztmd33vy573wobol.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YgBTLfFb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9nxztmd33vy573wobol.png" alt="Image description" width="875" height="318"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Preface.
&lt;/h2&gt;

&lt;p&gt;In this article i’ll talk about how to set up a NodeJS Express server application. At the end It should get across what NodeJS is, how to set up Express, and how to use GET requests using NodeJS.&lt;/p&gt;

&lt;p&gt;Also this article is part of a series where i describe how to use GET requests, POST requests, and serve static files.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is NodeJS?
&lt;/h2&gt;

&lt;p&gt;JavaScript is a popular programming language that has mostly been used in the browser, but with the invention of NodeJS we can run JavaScript away from the web browser; We can now run JavaScript in the command line! NodeJS is supported by Googles V8 Engine and is used by millions of developers everyday which means that NodeJS is here to stay. NodeJS also has its own package library called NPM that handles importing and publishing re-usable JavaScript modules. In summary NodeJS isn't going anywhere, is powerful, and can be used in many different ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use NodeJS?
&lt;/h2&gt;

&lt;p&gt;The reason many software web developers use NodeJS is because most web dev’s already know JavaScript from working in the front end by using technologies like React, Svelte, Vue, and a many other popular JavaScript frameworks. Many developers decide to use NodeJS also because of the fact that JavaScript is asynchronous, meaning at runtime code can execute if another piece of code is taking too long to run. Which means a faster and much more efficient way to build, and run software.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the NodeJS Express module?
&lt;/h2&gt;

&lt;p&gt;As I explained earlier NodeJS has a package manager built around it called NPM. And NPM can be used to download millions of modules that you can import into your project. One of these modules is called Express. Express is mostly used handle files when an HTTP request is received. Express can also do many other things, but we’ll stick to a simple GET request for now.&lt;/p&gt;

&lt;h2&gt;
  
  
  First lets create our super simple index.html file.
&lt;/h2&gt;

&lt;p&gt;Whats an html file you ask? An html file is the building block to all website (some people call this a skeleton of the web) and a browser knows how to interpret an HTML file in a way that makes it work with other files. HTML has become very powerful over the last few years, going from just simple text to now being able to download files, lazy-load recourse's, make get requests to other files, and much much more (ill probably make a html article soon also).&lt;/p&gt;

&lt;p&gt;But Anyway lets create our awesome HTML file now. Here it is. Just feel free to copy this to your index.html file and save it there.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ipCHzJBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6weaeusml225p6d5jwzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ipCHzJBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6weaeusml225p6d5jwzi.png" alt="Image description" width="875" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets install Express.
&lt;/h2&gt;

&lt;p&gt;Open up your command line after making sure that NPM has been successfully installed (NPM is the package manger for NodeJS and we’ll need it to download express). To make sure express is installed on your machine use npm -v And your terminal output should look something like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HfaaVRCO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwfkhtg9kwwg8ssz86um.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HfaaVRCO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwfkhtg9kwwg8ssz86um.png" alt="Image description" width="875" height="36"&gt;&lt;/a&gt;&lt;br&gt;
You should receive the version of NPM installed on your machine. After confirming that express is on your machine use this terminal command in the directory (directory also called folder) that your index.html file sits in.&lt;/p&gt;

&lt;p&gt;npm install express&lt;br&gt;
You should receive this output.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d_kcO4cF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vstl97my0qdrdczczhf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d_kcO4cF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vstl97my0qdrdczczhf.png" alt="Image description" width="875" height="124"&gt;&lt;/a&gt;&lt;br&gt;
and just like that you have installed all the modules required to start working with express!&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Express for a simple GET request.
&lt;/h2&gt;

&lt;p&gt;Lets say that we have an html file that’s sitting in our current directory like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IT6D18Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xhbx0rhoinfdf58ysr1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IT6D18Vb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xhbx0rhoinfdf58ysr1.png" alt="Image description" width="875" height="49"&gt;&lt;/a&gt;&lt;br&gt;
And we want Express to handle a GET request to a certain route in our URL and return our index.html file to the client that made that request. With Express this is made pretty simple. All you have to do is create a server.js (commonly called app.js) file, import the Express module, then initialize that class to an object variable. Then specify our variables listening point using the .listen(port, callback) method. All in all the code should look something like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7rvkJnPY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3lk4q5vdk4lc6img991.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7rvkJnPY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3lk4q5vdk4lc6img991.png" alt="Image description" width="875" height="193"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Now run the script!
&lt;/h2&gt;

&lt;p&gt;Now save the data to the server.js file and run node followed by the server.js file name. It should look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rxFttjoj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7d6eaeuwhotz9pegw5p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rxFttjoj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b7d6eaeuwhotz9pegw5p.png" alt="Image description" width="875" height="51"&gt;&lt;/a&gt;&lt;br&gt;
You receive our console output from the file. Then we’ll navigate to our web browser. And type localhost:8080. It should look like this.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--djxlxjHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufhr4ubtxp7gv3lnsbm6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--djxlxjHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufhr4ubtxp7gv3lnsbm6.png" alt="Image description" width="875" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  You did it! You’ve responded to the client (the browser) with your application!
&lt;/h2&gt;

&lt;p&gt;Good job! The amount of engineering that has come to this point is incredible and it took decades to get to this point! You can now make other html files and use GET requests to serve this files! Now for the problem serving a single file using GET. The problem with using one GET request is that only the specified file will be sent back to the client (this is called a response). What if the index.html links to a JavaScript file or a CSS file for it to work (you’ll need more get requests that link to those files!)? A simple solution would be host serve a static folder containtng all the files that you need for your application. I’ll talk about serving static folders in another article (coming soon!).&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank you for reading!
&lt;/h2&gt;

&lt;p&gt;Thanks for reading. I hope you found this article enjoyable and learned something in the process. If there’s something that you feel like I've left out then comment down below, or message me directly. Also leave a follow if you found this article informative! :)&lt;/p&gt;

&lt;h2&gt;
  
  
  What i’m up to lately.
&lt;/h2&gt;

&lt;p&gt;My names David and I've been working on software web development for about 2 years now. Mostly free lance work. I know Python, JavaScript, Bash, CSS, HTML, PostgreSQL, MongoDB, and many other technologies. If you want to get in touch check me out here at davidesquerra.com. Here you can text me directly or email me. And like i said above thanks for reading :).&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
