<?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: Florian H.</title>
    <description>The latest articles on DEV Community by Florian H. (@__florian).</description>
    <link>https://dev.to/__florian</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%2F190131%2F6c77334e-7ba8-4474-af03-7cf218f31f20.jpg</url>
      <title>DEV Community: Florian H.</title>
      <link>https://dev.to/__florian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/__florian"/>
    <language>en</language>
    <item>
      <title>UNIX pipes and output redirects</title>
      <dc:creator>Florian H.</dc:creator>
      <pubDate>Fri, 29 May 2020 11:15:53 +0000</pubDate>
      <link>https://dev.to/__florian/unix-pipes-and-output-redirects-2m65</link>
      <guid>https://dev.to/__florian/unix-pipes-and-output-redirects-2m65</guid>
      <description>&lt;p&gt;(&lt;em&gt;Initially published on &lt;a href="https://florianherlings.de/posts/2020-05-26-unix-pipes-and-filters-design-pattern/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;.&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Pipes and redirects are a core concept of using the command line in any UNIX like system&lt;sup id="fnref1"&gt;1&lt;/sup&gt;. With the new WSL&lt;sup id="fnref2"&gt;2&lt;/sup&gt; package UNIX like concepts are becoming more and more important for developers using Windows as well. Redirecting inputs and outputs is core to how the UNIX system was designed. The rise in popularity of functional programming&lt;sup id="fnref3"&gt;3&lt;/sup&gt; is a great reason to explore this concept again.&lt;/p&gt;

&lt;p&gt;This article will help you dig into the concept, and understand why pipes and redirects are so beautiful and versatile. You will be able to take this simple and beautiful design, and apply it to how you design your application's code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Commands
&lt;/h2&gt;

&lt;p&gt;When you run an application from your terminal, you are mostly interested in the output of your application. Say you run the &lt;code&gt;ls&lt;/code&gt; (or &lt;code&gt;dir&lt;/code&gt; on Windows), you expect to see a list of files in the current working directory.&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%2Fflorianherlings.de%2Fpublic%2F2020-05-26%2Fls.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%2Fflorianherlings.de%2Fpublic%2F2020-05-26%2Fls.png"&gt;&lt;/a&gt;&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt;

a.png
b.txt
c.txt
d.txt
e.jpg
f.jpg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is not much of a concept behind this, it is just the way you would expect things to work.&lt;/p&gt;

&lt;p&gt;You could even say, that in this scenario, there are two elements involved: The application &lt;code&gt;ls&lt;/code&gt;, which outputs the list of files from a directory, and the terminal&lt;sup id="fnref4"&gt;4&lt;/sup&gt;, which receives this output as its input and makes the letters appear on the screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pipes
&lt;/h2&gt;

&lt;p&gt;The concept behind pipes is very simple to grasp: You take two applications, and take the output of the first application, and connect it to the input of the second application. Just like in plumbing you would connect a water source to a faucet with a pipe. The second application, will do its work using the data you put into it's &lt;em&gt;input&lt;/em&gt;, and generate some kind of &lt;em&gt;output&lt;/em&gt;. This outpuf of the second application, will be sent to the input of the terminal, which again makes letters appear on the screen.&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%2Fflorianherlings.de%2Fpublic%2F2020-05-26%2Fls-grep.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%2Fflorianherlings.de%2Fpublic%2F2020-05-26%2Fls-grep.png"&gt;&lt;/a&gt;&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="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; | &lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="s2"&gt;"jpg"&lt;/span&gt;

e.jpg
f.jpg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our little example, the &lt;code&gt;ls -l&lt;/code&gt; command will output the list of files in the current directory (see above), and the command &lt;code&gt;grep "jpg"&lt;/code&gt; will act as a filter, that only allows lines that have "&lt;em&gt;jpg&lt;/em&gt;" in them, to pass through to the terminal. Syntactically this is achieved by putting a little vertical bar &lt;code&gt;|&lt;/code&gt; (called pipe character) between the two commands.&lt;/p&gt;

&lt;p&gt;You could also understand this setup as a list of commands, chained together. You can chain as many applications in a row as you like: As long as you always have some kind of output, to send to the next command as input, your chain will work perfectly fine.&lt;/p&gt;

&lt;p&gt;And this – I think – is the beauty of the UNIX pipes concept: No UNIX system comes with an app, that lets you output the list of jpgs in a current directory. But you can easily make this happen by chaining a command that can output all files to a command that can filter filenames including "&lt;em&gt;jpg&lt;/em&gt;".&lt;/p&gt;

&lt;p&gt;There are endless possibilities of what you could achieve with this kind of setup. Every new command you learn does not only give you the power of the specific command, but also allows you to use this command in context of other commands.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Reading logs
&lt;/h3&gt;

&lt;p&gt;Just to give you one example: Let's say you have a folder full of logs for all your services for the last 7 days. You want to see all the entries from all the files, that were collected at a certain hour of a certain day, and that include a special user id.&lt;/p&gt;

&lt;p&gt;First you probably want to output all the logs to see what they look like, using the &lt;code&gt;cat&lt;/code&gt;&lt;sup id="fnref5"&gt;5&lt;/sup&gt; command:&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;cat&lt;/span&gt; /var/logs/&lt;span class="k"&gt;*&lt;/span&gt;.log

2020-04-11 11:43:45 - INFO - Opened connection to service X. - UID 42
2020-04-11 11:43:45 - INFO - Opened connection to service Y. - UID 42
2020-04-11 11:43:47 - INFO - Opened connection to service X. - UID 32
2020-04-11 11:43:55 - ERR  - Connection timed out &lt;span class="k"&gt;for &lt;/span&gt;X. - UID 42
2020-04-12 11:56:32 - INFO - Sending confirmation email - UID 32
2020-04-13 22:56:32 - INFO - Sending confirmation email - UID 32
2020-04-14 09:12:22 - INFO - Sending confirmation email - UID 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, you only want logs from the 11th of April, so let's filter the output:&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;cat&lt;/span&gt; /var/logs/&lt;span class="k"&gt;*&lt;/span&gt;.log | grep &lt;span class="s2"&gt;"2020-04-11"&lt;/span&gt;

2020-04-11 11:43:45 - INFO - Opened connection to service X. - UID 42
2020-04-11 11:43:45 - INFO - Opened connection to service Y. - UID 42
2020-04-11 11:43:47 - INFO - Opened connection to service X. - UID 32
2020-04-11 11:43:55 - ERR  - Connection timed out &lt;span class="k"&gt;for &lt;/span&gt;X. - UID 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last, let's make sure to only output the rows for the user id 42:&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;cat&lt;/span&gt; /var/logs/&lt;span class="k"&gt;*&lt;/span&gt;.log | grep &lt;span class="s2"&gt;"2020-04-11"&lt;/span&gt; | grep &lt;span class="s2"&gt;"UID 42"&lt;/span&gt;

2020-04-11 11:43:45 - INFO - Opened connection to service X. - UID 42
2020-04-11 11:43:45 - INFO - Opened connection to service Y. - UID 42
2020-04-11 11:43:55 - ERR  - Connection timed out &lt;span class="k"&gt;for &lt;/span&gt;X. - UID 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though the command &lt;code&gt;cat /var/logs/*.log | grep "2020-04-11" | grep "UID 42"&lt;/code&gt; might look intimidating at first, it is actually quite simple. If you build up your command chain step by step, you have full control over what is happening and can build up the complexity of this command chain one step at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pipes and filters
&lt;/h2&gt;

&lt;p&gt;Understanding this concept is super helpful in doing almost anything with a UNIX like command line: You don't need to build specialized applications, that can do very specific things. You actually want the opposite: You want to make applications, that are very simple and versatile, so that they can be connected to other applications that are the same. This is the fundamental idea behind UNIX pipes.&lt;/p&gt;

&lt;p&gt;And this simple and beautiful concept can (and probably should) be applied to all kinds of challenges, that we try to solve with code: If you need to do a lot of data processing using a Jupyter notebook&lt;sup id="fnref6"&gt;6&lt;/sup&gt;, you are basically doing the same thing: You build up your data cleaning and filtering pipeline, one step at a time.&lt;/p&gt;

&lt;p&gt;If you are building sophisticated Javascript applications, in a "functional" way, you also follow this principle (or at least you should ;)&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="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;lda&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&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;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This design principal allows you to design beautiful and well structured applications, and at least in my opinion, it is a great way to think about the problem you are trying to solve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirects
&lt;/h2&gt;

&lt;p&gt;Files are so special, that people usually want to have a way to deal with them in a very convenient way. This is why UNIX has two special "&lt;em&gt;signs&lt;/em&gt;", which allow to either write to a file, or append to a file.&lt;/p&gt;

&lt;p&gt;Let's say you want to write the output of some command you write to put into a file. I will use our logging example from above:&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;cat&lt;/span&gt; /var/logs/&lt;span class="k"&gt;*&lt;/span&gt;.log | grep &lt;span class="s2"&gt;"2020-04-11"&lt;/span&gt; | grep &lt;span class="s2"&gt;"UID 42"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; output.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;gt; output.txt&lt;/code&gt; at the end of the line will tell the system to write the generated output from the last command in the chain to a file called "&lt;em&gt;output.txt&lt;/em&gt;". If this file does not exist, it will just create a file and put the content in their. Notice, that running this line, will no longer give you any output in your terminal, because you &lt;em&gt;redirected&lt;/em&gt; the output into a file.&lt;/p&gt;

&lt;p&gt;This "&lt;em&gt;sign&lt;/em&gt;" also has a sibling, which will keep the current content of the mentioned file, and just append the new output at the end. &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; will append, instead of replace the content of that file.&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;cat&lt;/span&gt; /var/logs/&lt;span class="k"&gt;*&lt;/span&gt;.log | grep &lt;span class="s2"&gt;"2020-04-11"&lt;/span&gt; | grep &lt;span class="s2"&gt;"UID 42"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; output.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just like the previous command, our new example ending in &lt;code&gt;&amp;gt;&amp;gt; output.txt&lt;/code&gt; will show no output on the terminal, as the output is – again – redirected away from the terminal, into your file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;So to wrap this up: One of the core UNIX principles is to connect the output of one simple and versatile application to the input of another simple and versatile application. As a result, it is very simple, to build complex functionality right from your terminal, by just "chaining" applications together.&lt;/p&gt;

&lt;p&gt;You can find this principle in many parts of software development, from number crunching to developing sophisticated functional software.&lt;/p&gt;

&lt;p&gt;I hope this little article helped you understand this core UNIX principle. As always, I am happy to hear about your thoughts and opinions.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;This includes all UNIXes like openBSD or MacOSX and Linux distributions. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;WSL is short for &lt;em&gt;Windows Subsystem for Linux&lt;/em&gt;. It is a recent (as of 2020) addition to the Windows operating system and is still under development (Version 2 was just released). It basically is a stripped down version of Linux, running alongside Windows, allowing you to run most Linux applications, especially command line applications. This is a great help for developers, who are using Windows to develop in an environment that makes heavy use of the Linux or UNIX ecosystem. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;Functional programming is a concept that is very old, but became popular again in recent years. It is very hard to boil it down into a footnote, but the basic concept is that you try to write your application as a series of functions, that follow a strict ruleset, which ensures that these functions do not create any side effects: They are supposed to receive input and return output based on that. If I call a function with the input "&lt;em&gt;X&lt;/em&gt;" today, it should yield the exact same result as it will tomorrow or in two years. There are many benefits for this, and I recommend digging deeper into this starting at &lt;a href="https://en.wikipedia.org/wiki/Functional_programming" rel="noopener noreferrer"&gt;the Wikipedia article about functional programming&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;Terminal is a term commonly used for an application that allows you to control a computer via text input. There is a lot of history behind it, which is why this term is not 100% correct in many use cases, but it is the term most commonly used. The well-known desktop and server operating systems (Linux, Windows, MacOSX) all come with some kind of Terminal. Mobile operating systems often offer some kind of app to allow you to at least connect to another computer's system via text input. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; is a very simple unix command, which allows to output the content of one or more files. If you don't pipe its output into another command, it will just output a file's content onto the terminal. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;a href="https://jupyter.org/" rel="noopener noreferrer"&gt;Jupyter notebooks&lt;/a&gt; are a system that is heavily used by data scientists and analysts all over the world, to do data analysis. It will allow you to write Python code that loads and processes data and prints graphs. The beauty of it is its nice and simple graphical user interface and portability. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
    </item>
    <item>
      <title>JAM stack – what is it about, and should you consider using it for your next project?</title>
      <dc:creator>Florian H.</dc:creator>
      <pubDate>Mon, 25 May 2020 11:01:55 +0000</pubDate>
      <link>https://dev.to/__florian/jam-stack-what-is-it-about-and-should-you-consider-using-it-for-your-next-project-1300</link>
      <guid>https://dev.to/__florian/jam-stack-what-is-it-about-and-should-you-consider-using-it-for-your-next-project-1300</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published on &lt;a href="https://florianherlings.de/posts/2020-05-21-jam-stack-javascript-apis-markup/"&gt;my blog&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;JAM stack is a concept, that emerged some time in late 2019 or early 2020. It takes the already established concepts of cloud native web development and SPAs&lt;sup id="fnref1"&gt;1&lt;/sup&gt; one step further. There is nothing technically new in JAM (which stands for &lt;em&gt;Javascript&lt;/em&gt;, &lt;em&gt;APIs&lt;/em&gt; and &lt;em&gt;Markup&lt;/em&gt;), but by applying a new view on the combination of those technologies, it defines a new (and kind of liberating) approach to building web based applications.&lt;/p&gt;

&lt;p&gt;I am trying to give a gentle introduction into what the JAM stack actually is, and try to share my view on how and why it is important to developers. Reading this article should allow you to have an informed opinion about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a JAM stack?
&lt;/h2&gt;

&lt;p&gt;From a high level perspective, the JAM stack is limiting the technologies you use to three main building blocks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Javascript&lt;/li&gt;
&lt;li&gt;APIs&lt;/li&gt;
&lt;li&gt;Markup&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the idea is to use Markup (probably HTML) as a basic foundation and scaffold around your application, fill in the interactive functionality (potentially using SPA frameworks&lt;sup id="fnref1"&gt;1&lt;/sup&gt;) using Javascript and connect to server functionality (like persistence, heavy calculation, payment handling, …) via an API.&lt;/p&gt;

&lt;p&gt;What you would not do (among other things) is to render dynamic content on your web server (using some kind of backend framework&lt;sup id="fnref2"&gt;2&lt;/sup&gt; or more modern approach like &lt;a href="https://nextjs.org/"&gt;next.js&lt;/a&gt;). The only role of the web servers in a JAM stack application is to provide an API for their functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classic website approach
&lt;/h2&gt;

&lt;p&gt;To get a better perspective on the matter, let's look at a more classic approach to building websites or web apps.&lt;/p&gt;

&lt;p&gt;You probably have a bunch of different building blocks, like static files (css, js or images), a database and probably some kind of caching system. You also might use some internal or cloud APIs to get additional functionality (like providing your users with credit card payment etc).&lt;/p&gt;

&lt;p&gt;The classic way to build a website like this is to build a big node.js application, which does all the heavy lifting by connecting the different sources of data and functionality and renders nice looking pages for the browser to display.&lt;/p&gt;

&lt;p&gt;Whenever a user interacts with the page (by clicking a link or submitting a form), a request will be sent to the server, it will generate a new page (probably using a database) and sent it back to the user's browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XuGaMeRq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-05-21/classic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XuGaMeRq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-05-21/classic.png" alt="Diagram picturing multiple backend services, a node.js instance and a browser."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is – of course – an extremely classical approach, but I wanted to have something to contrast the JAM stack approach to.&lt;/p&gt;

&lt;h2&gt;
  
  
  JAMstack approach to building a website
&lt;/h2&gt;

&lt;p&gt;Building a website using the JAM stack approach limits the technologies we can actually use to a set of simple and manageable tools: Javascript, APIs and Markup.&lt;/p&gt;

&lt;h3&gt;
  
  
  Markup
&lt;/h3&gt;

&lt;p&gt;Most websites and web applications have a good portion of basically "static" markup. Think of your skeleton HTML and maybe your markdown&lt;sup id="fnref3"&gt;3&lt;/sup&gt;, that form your "basic" content pages, like the contact or about pages. There is nothing dynamic about them, no need to update them constantly, and no need to run a CMS to generate those.&lt;/p&gt;

&lt;p&gt;You can either create these HTML files manually, or use some kind of static site generator&lt;sup id="fnref4"&gt;4&lt;/sup&gt; to generate those for you. This markup will then be uploaded to some kind of simple web server or CDN&lt;sup id="fnref5"&gt;5&lt;/sup&gt;, together with any static assets you might have, like your CSS files of your logo.&lt;/p&gt;

&lt;p&gt;Once this is generated and uploaded, it will only ever change, when you decide to deploy updates manually.&lt;/p&gt;

&lt;h3&gt;
  
  
  APIs
&lt;/h3&gt;

&lt;p&gt;If you build anything but the simplest brochure website&lt;sup id="fnref6"&gt;6&lt;/sup&gt;, you probably want to run code on your server(s), that provides some kind of unique functionality. Be it saving your user's data in your database, or adding funny bunny ears to your user's profile picture.&lt;/p&gt;

&lt;p&gt;Following the JAM stack approach, mean to put this functionality behind some kind of API, probably using HTTP endpoints&lt;sup id="fnref7"&gt;7&lt;/sup&gt;. The API code can then be deployed in the way you (or your company) prefers, be it on your own hardware in a data center somewhere or as using a FaaS&lt;sup id="fnref8"&gt;8&lt;/sup&gt; service.&lt;/p&gt;

&lt;p&gt;Additionally to your own APIs, you can (and maybe should) use external APIs provided by other companies. These external APIs, could add any functionality to your application, from a headless CMS&lt;sup id="fnref9"&gt;9&lt;/sup&gt; to a payment provider. A beautiful side effect of this is, that in case you want to exchange one provider for another (or even an internal API you built) becomes a lot simpler, as there is only one place where all the APIs are connected: your Javascript running in the user's browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript
&lt;/h3&gt;

&lt;p&gt;The third (and last) building block is Javascript. All the dynamic functionality of your application, from loading your latest tweets to allowing user's to pay for the T-Shirt they want to buy, will be encapsulated in your Javascript code.&lt;/p&gt;

&lt;p&gt;By doing that, your Javascript code will be the most important part of your application, and will probably need the most attention. Luckily, there are many great libraries, that help to build complex Javascript SPAs&lt;sup id="fnref1"&gt;1&lt;/sup&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--if485gkn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-05-21/jam.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--if485gkn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-05-21/jam.png" alt="Diagram picturing the JAM stack approach."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits
&lt;/h2&gt;

&lt;p&gt;The most interesting question remains: Why should anyone use this? It certainly is true, that limiting one's choices already is a benefit in itself, as it forces us to be more creative in our approach.&lt;/p&gt;

&lt;p&gt;Most of the benefits of using the JAM stack revolve around its simplicity and lack of things. If you don't have a backend server which partially generates your website's content, you will not end up with a big messy ball of mud&lt;sup id="fnref10"&gt;10&lt;/sup&gt;, that becomes unmaintainable after a few years.&lt;/p&gt;

&lt;p&gt;If you don't generate any part of your website "on the fly" for your users, and instead put your application (except for your APIs) on a CDN, hosting will be very cheap because you do not need to run expensive servers. Even in peak times, the CDN will just handle the distribution of your application, so that even Black Friday does not have to scare you anymore.&lt;/p&gt;

&lt;p&gt;There is also the benefit of security: The only way to have a secure server, is to not have a server at all. Everything that exists can be hacked, so better not have the server exist at all. 😉 The APIs you will write for your JAM stack application need to be accessible by the user's browser, so you are basically forced to secure them properly, instead of hiding them behind some kind of load balancer / firewall setup.&lt;/p&gt;

&lt;p&gt;It is also true, that the JAM stack approach makes it less hard and/or scary to deploy a change to production: What you deploy is "just" a set of static files, containing your markup and Javascript. So if anything goes sideways with a new release, it is very simple to "roll back" and redeploy the old files.&lt;/p&gt;

&lt;p&gt;There are certainly more benefits to using the JAM stack, and I would be happy to extend this list with your ideas. Do not hesitate to get in contact with me via &lt;a href="https://twitter.com/__florian"&gt;twitter.com/__florian&lt;/a&gt; or &lt;a href="https://florianherlings.de/pages/contact/"&gt;email&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;What I intent to express is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JAM stack stands for: &lt;strong&gt;J&lt;/strong&gt;avascript, &lt;strong&gt;A&lt;/strong&gt;PIs and &lt;strong&gt;M&lt;/strong&gt;arkup&lt;/li&gt;
&lt;li&gt;It very much reduces the technology choices you can make&lt;/li&gt;
&lt;li&gt;It takes a lot of emphasis away from classic server backend driven approaches&lt;/li&gt;
&lt;li&gt;Some of the benefits are scalability and security&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope that you did not only enjoy reading this article, but could also learn something valuable from it. Thank you for spending your time on reading my words.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://jamstack.wtf/"&gt;jamstack.wtf&lt;/a&gt; is a great short website explaining the topic in a very digestible way&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://jamstack.org/"&gt;jamstack.org&lt;/a&gt; is a wonderful learning resource&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://snipcart.com/blog/jamstack"&gt;snipcart.com/blog/jamstack&lt;/a&gt; is a longer blog article about this very topic&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;SPA stands for Single Page Application. The idea is that your application does not use the traditional model of splitting up your application into many pages and have the user navigate from one page to another using links. The SPA approach is to have one single page, and dynamically update or replace parts of the page, depending on what the user is trying to achieve. Popular SPA frameworks (as of May 2020) are &lt;a href="//reactjs.org/"&gt;React.js&lt;/a&gt;, &lt;a href="https://angular.io/"&gt;Angular.js&lt;/a&gt; or &lt;a href="https://vuejs.org/"&gt;Vue.js&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;There are many "&lt;em&gt;backend frameworks&lt;/em&gt;", and they exist for almost any programming language. Popular frameworks are &lt;a href="http://expressjs.com/"&gt;Express.js&lt;/a&gt;, &lt;a href="https://rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt;, &lt;a href="https://symfony.com/"&gt;Symfony (PHP)&lt;/a&gt; or &lt;a href="https://dotnet.microsoft.com/"&gt;.NET (C#)&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;Markdown is a very simple way to write text, that will later be turned into HTML. Instead of writing tags like &lt;code&gt;&amp;lt;strong&amp;gt;this one&amp;lt;/strong&amp;gt;&lt;/code&gt;, you simply use special characters like &lt;code&gt;*this one*&lt;/code&gt; to indicate that a text should be emphasized, strong, a table etc. Markdown was invented by John Grober, and you can read more about &lt;a href="https://daringfireball.net/projects/markdown/"&gt;Markdown on his website&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;The idea behind static site generators is, that even though some websites really need a CMS to manage all their content, many websites don't. This website for example, does not change very often: It only changes whenever I write and publish a new article. Static site generators are wonderful tools, that basically take a directory full of content files (like these articles), combine them with a layout (like the one you see) and output a new directory, with fully rendered beautiful HTML pages. This resulting directory can then easily be deployed to any kind of simple web server. This makes hosting a statically generated website very cheap and secure (because there is no dynamic code or database to attack). This website is generated using the open source tool &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;, and the website &lt;a href="https://www.staticgen.com/"&gt;StaticGen&lt;/a&gt; has a great list of alternatives. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;CDN stands for Content Delivery Network. To put it simply, a CDN tries to make static files (like CSS or your logo) available to your website's users as quickly as possible. To achieve this, it usually consists of many servers distributed all over the planet. When you upload a file to the CDN, all the distributed servers will receive a copy of your file. Later, when a user wants to see that file (let's say the logo on your website), the CDN will deliver it from the server that is closest to your user. If you – for example – live in central Europe and upload your logo to a CDN, a website visitor from Korea will receive this file from a server in Korea, not central Europe. This improves loading times a lot. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;&lt;em&gt;Brochure website&lt;/em&gt; is a term often used for websites that do not accept any input from users, and are instead just displaying information, generated by the people who run the website. This was very popular in the early days of the internet, and is still used in many occasions. Think about a product website for a new phone or computer: The page will give you a lot of information, but you cannot contribute anything. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;Endpoints are ways to access functionality on the server, using the network (mostly the internet) to send to and receive data from. There are technologies that can be used to achieve this. The ones that are most often used (as of May 2020) are: &lt;a href="https://en.wikipedia.org/wiki/Representational_state_transfer"&gt;REST&lt;/a&gt;; &lt;a href="https://en.wikipedia.org/wiki/GraphQL"&gt;GRAPHQL&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSocket"&gt;Web sockets&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;&lt;em&gt;Functions as a Service&lt;/em&gt; is a new approach to building functionality that would previously be build using backend server technology. The basic idea behind it is that every piece of functionality is bundled up and deployed as a single unit to some kind of service provider. You – as the developer – do not need to worry about how and where this function is actually running: The service will provide you with a URL that you can call to get to your function. The rest is abstracted away from you. The technology behind this is still relatively young, but it has potential to be a good tool for very special use cases. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn9"&gt;
&lt;p&gt;The idea behind a headless CMS is quite clever: The CMS will provide you with a wonderful and easy to use backend, which can be used by you and your team to create and structure content and probably even upload assets. While a "classic" CMS will also be able to turn your content into a beautiful website, a headless CMS will only provide endpoints&lt;sup id="fnref7"&gt;7&lt;/sup&gt;, that you can use with your code to receive the data. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn10"&gt;
&lt;p&gt;&lt;em&gt;Big ball of mud&lt;/em&gt; is a term &lt;a href="http://www.laputan.org/mud/mud.html#BigBallOfMud"&gt;coined by Brian Foote and Joseph Yoder&lt;/a&gt; to describe a piece of software, that consists of code that is completely unstructured and has an endless amount of undocumented connections between components. Software like that is very hard (and expensive) to maintain, and very unpopular among developers. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>markup</category>
      <category>api</category>
      <category>jam</category>
    </item>
    <item>
      <title>What is CRUD and how does it help me with my application?</title>
      <dc:creator>Florian H.</dc:creator>
      <pubDate>Wed, 29 Apr 2020 07:16:37 +0000</pubDate>
      <link>https://dev.to/__florian/what-is-crud-and-how-does-it-help-me-with-my-application-18b1</link>
      <guid>https://dev.to/__florian/what-is-crud-and-how-does-it-help-me-with-my-application-18b1</guid>
      <description>&lt;p&gt;(Originally published at &lt;a href="https://florianherlings.de/posts/2020-04-28-crud-create-read-update-delete/"&gt;florianherlings.de&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Some concepts are not really hard to understand, but so core to the work of a developer, that it is worth thinking and writing about them. CRUD is one of those concepts: You will find many developers talking about CRUD in all kinds of programming projects and tasks, as a way to express what they are thinking about a feature.&lt;/p&gt;

&lt;p&gt;You might find somebody saying: „&lt;em&gt;Twitter is just CRUD without the U in front of a big tweets table with joined users.&lt;/em&gt;” and even if this is an extreme simplification, it still expresses a lot of information in a small sentence. Many programmers love to speak like this. 😅&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept
&lt;/h2&gt;

&lt;p&gt;CRUD is actually a pretty simple concept: It describes a set of actions you can do to your data. Whenever you are handling data (in a data base, on your hard drive, in the "&lt;em&gt;cloud&lt;/em&gt;"), you always want to think about: What can a user do to this data.&lt;/p&gt;

&lt;p&gt;It usually falls into one of these four actions that can be performed to a data point&lt;sup id="fnref1"&gt;1&lt;/sup&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can &lt;strong&gt;create&lt;/strong&gt; a new data point&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;read&lt;/strong&gt; existing data points&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;update&lt;/strong&gt; an existing data point&lt;/li&gt;
&lt;li&gt;You can &lt;strong&gt;delete&lt;/strong&gt; an existing data point&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And this is exactly where this acronym is coming from. CRUD is short for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;REATE&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;EAD&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;U&lt;/strong&gt;PDATE&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt;ELETE&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So any application you write, that is dealing with data, can do one of those four things: Create, Read, Update and Delete. Let's look at a few use cases like SQL, the file system and HTTP.&lt;/p&gt;

&lt;h3&gt;
  
  
  SQL (like PostgreSQL or MySQL)
&lt;/h3&gt;

&lt;p&gt;SQL is the super powerful language, that will help you to "talk" to a relational database&lt;sup id="fnref2"&gt;2&lt;/sup&gt; like PostgreSQL. All you can do to your database's data is adding new entries, getting entries, updating and deleting entries. &lt;/p&gt;

&lt;p&gt;Here are some example SQL queries, and what their role in CRUD is:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;SQL query&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;C&lt;/strong&gt;reate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;INSERT INTO users (name) VALUES ('Alice');&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;ead&lt;/td&gt;
&lt;td&gt;&lt;code&gt;SELECT * FROM users;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;U&lt;/strong&gt;pdate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;UPDATE users SET name='Bob' WHERE id=1;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;elete&lt;/td&gt;
&lt;td&gt;&lt;code&gt;DELETE FROM users WHERE id=2;&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  File system
&lt;/h3&gt;

&lt;p&gt;The file system follows the same set of rules: You can create, read, update and delete files. Every action you can do to a file falls into one of those four categories.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Shell command&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;C&lt;/strong&gt;reate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;touch hello.txt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;ead&lt;/td&gt;
&lt;td&gt;&lt;code&gt;cat hello.txt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;U&lt;/strong&gt;pdate&lt;/td&gt;
&lt;td&gt;&lt;code&gt;echo "Hi" &amp;gt; hello.txt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;elete&lt;/td&gt;
&lt;td&gt;&lt;code&gt;rm hello.txt&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Even moving a file from one directory to another directory is just an update, as you only update it's path from one directory to another.&lt;/p&gt;

&lt;p&gt;If you run: &lt;code&gt;mv hello.txt ~/Desktop/hello.txt&lt;/code&gt; it will move the file &lt;em&gt;hello.txt&lt;/em&gt; from its current location to the desktop, which only means that the file's path will be updated to the new directory (&lt;code&gt;~/Desktop&lt;/code&gt;),&lt;/p&gt;

&lt;h3&gt;
  
  
  HTTP
&lt;/h3&gt;

&lt;p&gt;Even HTTP&lt;sup id="fnref3"&gt;3&lt;/sup&gt; follows this concept. The main four HTTP actions, that a request can have are GET, PUT, POST and DELETE. Those can be easily categorized into the four CRUD actions as well:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;HTTP Method&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;C&lt;/strong&gt;reate&lt;/td&gt;
&lt;td&gt;POST&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;R&lt;/strong&gt;ead&lt;/td&gt;
&lt;td&gt;GET&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;U&lt;/strong&gt;pdate&lt;/td&gt;
&lt;td&gt;PUT&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;D&lt;/strong&gt;elete&lt;/td&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are numerous other examples of CRUD being used in all corners of software development. Those three (SQL, the file system and HTTP) are just simple examples to underline the point I am trying to make in this article: &lt;/p&gt;

&lt;p&gt;CRUD is everywhere. 😊&lt;/p&gt;

&lt;h2&gt;
  
  
  Using CRUD as a thinking model in your project
&lt;/h2&gt;

&lt;p&gt;The CRUD concept is – as we have seen – very simple and straight forward. Because of its simplicity we can use it as a foundation for thinking about how we design our software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data layer design
&lt;/h3&gt;

&lt;p&gt;Let's say we want to design a data layer for our new web application. The first thing we have to think about are the "Nouns", which means: What kind of "things" exist on our website.&lt;/p&gt;

&lt;p&gt;If you were to re-create something like twitter, you would definitely come up with two things: Tweets and users.&lt;/p&gt;

&lt;p&gt;The next step is to think about: What can you do with those Tweets and users. This is where CRUD comes in. For every "thing" that exists in your data layer (tweets and users), think about if you want to build and what kind of functionalities these would represent:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Tweet&lt;/th&gt;
&lt;th&gt;User&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Create&lt;/td&gt;
&lt;td&gt;Write a tweet.&lt;/td&gt;
&lt;td&gt;Register.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read&lt;/td&gt;
&lt;td&gt;See tweets.&lt;/td&gt;
&lt;td&gt;Login and show user profile.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update&lt;/td&gt;
&lt;td&gt;Edit a tweet.&lt;/td&gt;
&lt;td&gt;Change user profile.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;Remove a tweet.&lt;/td&gt;
&lt;td&gt;Delete account.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With this big list, you can think about which kind of functionality you want to provide to your users. In our example of Twitter, you can do almost all those things, except editing a tweet&lt;sup id="fnref4"&gt;4&lt;/sup&gt;. So the product team at Twitter sat down with this list and decided to not allow updating tweets.&lt;/p&gt;

&lt;h3&gt;
  
  
  API design
&lt;/h3&gt;

&lt;p&gt;The same model can be applied when you create an API for your application. Let's say you are working for Spotify and want to create an API for their app.&lt;/p&gt;

&lt;p&gt;The "things" in their app that come to mind are: Users, Songs, Albums and Playlists.&lt;/p&gt;

&lt;p&gt;For a user, the app API should pretty much allow everything:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;User&lt;/th&gt;
&lt;th&gt;Allowed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Create&lt;/td&gt;
&lt;td&gt;Register.&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read&lt;/td&gt;
&lt;td&gt;Login and show user profile.&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update&lt;/td&gt;
&lt;td&gt;Change user profile.&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;Delete account.&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;But what about a Song? It would not be helpful, if any user could create new songs, or delete existing ones.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Song&lt;/th&gt;
&lt;th&gt;Allowed&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Create&lt;/td&gt;
&lt;td&gt;Upload new song.&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read&lt;/td&gt;
&lt;td&gt;Listen to a song.&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update&lt;/td&gt;
&lt;td&gt;Change a song's title.&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete&lt;/td&gt;
&lt;td&gt;Delete a song.&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With this simple "matrix", you can go through all the "things" in your application and decide which of the four CRUD actions you want to allow.&lt;/p&gt;

&lt;p&gt;Thinking this through will give you a great foundation to talk to customers and users about your application as well as a great todo list. Even if you decide that a user should not be able to do some of the CRUD actions, this is still a valuable thing to know and a great thing to decide before you start implementing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code structure
&lt;/h3&gt;

&lt;p&gt;As we now know, almost all applications follow the CRUD concept. Even if you decide to not include one or more of the CRUD actions, the general concept is still the same.&lt;/p&gt;

&lt;p&gt;This is why, it makes a lot of sense to structure your application in the same way: You probably have routes to create, read, update and delete the things in your database, and you might want to name those routes in the same way.&lt;/p&gt;

&lt;p&gt;This means, that potentially, a route to add a new tweet might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/tweets/create&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newTweet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createTweet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTweet&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;Because this is such a well known pattern, many web frameworks even come with libraries to support creating CRUD actions for your data.&lt;/p&gt;

&lt;p&gt;Ruby on Rails&lt;sup id="fnref5"&gt;5&lt;/sup&gt; is the most famous example. It has special commands you can run to generate everything you need to do CRUD for a certain "thing" on your website. The generated code will including routes, controllers, models and database migrations&lt;sup id="fnref6"&gt;6&lt;/sup&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;rails generate scaffold Tweet text:string
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;What I hope you take from this article is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CRUD stands for create, read, update and delete&lt;/li&gt;
&lt;li&gt;Those four actions can be found everywhere in software development.&lt;/li&gt;
&lt;li&gt;When you think about the application you want to build, you can use CRUD as a guideline to think through the different "things" and what a user should or should not be able to do with them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you enjoyed reading the article and were able to learn something from it.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;In this article, I am using the word "&lt;em&gt;data point&lt;/em&gt;" as what you could think of as a single thing in your database. Like one user, one song or one Album. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Databases have existed for a very long time in the software development word. This is why many different kinds of databases exist to be provide a perfect solutoin for all the different use cases. The kind of database, that is used most often is called a relational database. If you allow me to simplify the matter a lot, you could think of a relational database as something like a fancy excel file. You have columns to define what kind of fields you want and rows to hold the data entries. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;HTTP is short for "&lt;em&gt;Hyper text transfer protocol&lt;/em&gt;" and is one of the basic foundations of the internet that we know today. This protocol defines how computers (often a browser and a server) communicate with each other. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;As a side note: On Twitter, you cannot actually edit a tweet, you can only delete it and write a new one. The developers and product people at Twitter certainly had/have their reasons to not allow editing a tweet, but many users still complain about the fact from time to time. 🤷‍♀️ ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;&lt;a href="https://rubyonrails.org/"&gt;Rails&lt;/a&gt; (actually &lt;em&gt;Ruby on Rails&lt;/em&gt;) is a very popular web framework, written in the programming language Ruby. It is know for its ease of use. There are frameworks for other languages that try to achieve similar goals. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;A database migration is a piece of code, that allows you to update the schema of the tables in your database. This means, that when you have an existing database with data in tables, you don't log into the server and change the table structure manually, but you have a well structured file with the code to do exactly that. Two of the biggest benefits are that you can make those changes on many different computers (like your own computer, your colleagues computer and the server). The other big benefit is that these migrations can often be undone, which helps a lot, if you find out later that you made a little mistake. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>crud</category>
      <category>sql</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Working with history, pushState and replaceState in Javascript</title>
      <dc:creator>Florian H.</dc:creator>
      <pubDate>Fri, 24 Apr 2020 11:43:10 +0000</pubDate>
      <link>https://dev.to/__florian/working-with-history-pushstate-and-replacestate-in-javascript-4h80</link>
      <guid>https://dev.to/__florian/working-with-history-pushstate-and-replacestate-in-javascript-4h80</guid>
      <description>&lt;p&gt;(Originally published on &lt;a href="https://florianherlings.de/posts/2020-04-22-history-javascript-pushstate/" rel="noopener noreferrer"&gt;florianherlings.de&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;The browser's history feature is something that we use almost every day, without thinking about it too much. When we navigate from one website to another website, we are creating basically a list of websites that we went to. Whenever we find ourselves in a situation where we want to go back to the previous page (or even a few pages back) we can easily do this with the &lt;em&gt;back&lt;/em&gt; button in our browser.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fbrowser.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fbrowser.png" alt="Screenshot of a browser with the back and forward buttons visible."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Javascript API&lt;sup id="fnref1"&gt;1&lt;/sup&gt; to deal with the browser's history is surprisingly easy to use. On the one hand we can move back and forth through the browser's history and on the other hand we can even manipulate the current and future state.&lt;/p&gt;
&lt;h2&gt;
  
  
  Moving back and forward
&lt;/h2&gt;

&lt;p&gt;Imagine our browser's history as a series of elements: There is always one element, that is our "current" element and it represents the page that we are currently seeing.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_base.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_base.png" alt="History example with three pages: Home, About and Contact"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Fortunately for us, the functionality behind the &lt;em&gt;back&lt;/em&gt; and &lt;em&gt;forward&lt;/em&gt; buttons is provided to us by the browser's &lt;code&gt;history&lt;/code&gt; object. To go back to the previous page, we can simple call &lt;code&gt;history.back()&lt;/code&gt; in our Javascript code and the browser will go "one element back" in our history. This way, the previous element will now be marked as the "current" element.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_back.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_back.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The browser does not forget about the other elements, so that we can also go forward and make the next element the "current" one by simple calling the &lt;code&gt;history.forward()&lt;/code&gt; method.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_forward.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_forward.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can even go multiple steps at a time (in either direction) using the &lt;code&gt;history.go()&lt;/code&gt; function. If you provide the value &lt;code&gt;1&lt;/code&gt; it will go forward one element, while calling &lt;code&gt;history.go(-2)&lt;/code&gt; will go two elements back.&lt;sup id="fnref2"&gt;2&lt;/sup&gt;&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_go.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_go.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conveniently, calling &lt;code&gt;history.go(0)&lt;/code&gt; will not go anywhere, and will instead just reload the page.&lt;/p&gt;
&lt;h2&gt;
  
  
  Changing history
&lt;/h2&gt;

&lt;p&gt;Going back and forth between existing entries in our browser's history is exciting and useful. The browser's history API&lt;sup id="fnref1"&gt;1&lt;/sup&gt; goes even further and allows us to add new entries or manipulate (to an extend) the entries that already exists. This is a great feature that enables authors of SPA&lt;sup id="fnref3"&gt;3&lt;/sup&gt; frameworks to write wonderful things like the &lt;a href="https://www.npmjs.com/package/react-router" rel="noopener noreferrer"&gt;React router&lt;/a&gt; library.&lt;/p&gt;
&lt;h3&gt;
  
  
  history.pushState
&lt;/h3&gt;

&lt;p&gt;The browser provides a way for us to add a new entry into the browser's history. Right now all major browser support this feature, but (as of 2020) it is still not 100% where it can be.&lt;/p&gt;

&lt;p&gt;Using the browser's &lt;code&gt;history.pushState&lt;/code&gt; function, we can add a new entry as the "current" entry of the history list. This way we will have added a new entry and at the same time updated what is our current entry at the same time.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_pushstate.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_pushstate.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The function itself takes three arguments: a state, a title and an URL:&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;const&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;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kd"&gt;const&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="s1"&gt;My new page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/new-page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pushState&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example will add a new entry into our history, with the state of an object carrying the user's id, the new title &lt;code&gt;My new page&lt;/code&gt; and the URL &lt;code&gt;/new-page&lt;/code&gt;. The &lt;code&gt;state&lt;/code&gt; parameter is really meant for those people who write libraries that make good use of it, because the browser itself will not do anything with this data. Unfortunately, the &lt;code&gt;title&lt;/code&gt; parameter is ignored by modern browsers (as of 2020), but &lt;em&gt;in theory&lt;/em&gt;  the title of the tab should be updated.&lt;/p&gt;

&lt;p&gt;This is why you often see code, uses the &lt;code&gt;null&lt;/code&gt; value for the first two parameters like so:&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="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pushState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/other-page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser will make good use of the last parameter &lt;code&gt;url&lt;/code&gt;, though: It will update the address bar and show our new URL.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fbrowser_after_state.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fbrowser_after_state.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One thing is interesting, though: It does not reload the content of the page. The browser will not actually go to the provided URL &lt;code&gt;/new-page&lt;/code&gt;. And this is the wonderful thing about &lt;code&gt;history.pushState&lt;/code&gt;: It will leave the currently displayed page as is, while updating the browser's address bar with the new URL. It adds a new history entry, without changing what is currently on the page.&lt;/p&gt;

&lt;p&gt;As a side note: If I actually wanted to go to the page (meaning: Also load the content of the page), I could easily just call &lt;code&gt;window.location='/new-page';&lt;/code&gt; to have the browser load that new page.&lt;/p&gt;

&lt;p&gt;Because we are manipulating the browser's history, we can still use the &lt;em&gt;back&lt;/em&gt; button in our browser to move back to the previous URL without any problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  history.replaceState
&lt;/h3&gt;

&lt;p&gt;Another way to change your browser's history state is to use the &lt;code&gt;replaceState&lt;/code&gt; function. It works almost exactly as the &lt;code&gt;pushState&lt;/code&gt; method mentioned above. The big difference is, that while &lt;code&gt;pushState&lt;/code&gt; will create a new entry in the browser's history, &lt;code&gt;replaceState&lt;/code&gt; will onle replace the current state.&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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_replacestate.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%2Fflorianherlings.de%2Fpublic%2F2020-04-22%2Fhistory_replacestate.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a side effect of this, using the &lt;code&gt;replaceState&lt;/code&gt; method will change the URL in the address bar, without creating a new history entry. Even though this was not the use case the developers had in mind, you could use this to change the URL in the address bar without any further side effects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;The browser's history is not only a useful feature for actual users, but also for us developers. You can navigate &lt;em&gt;back&lt;/em&gt; and &lt;em&gt;forward&lt;/em&gt;, and even &lt;em&gt;go&lt;/em&gt; multiple steps at a time. The browser's API will also allow you to manipulate the history state by adding a new entry &lt;code&gt;pushState&lt;/code&gt; or overwrite the current one using &lt;code&gt;replaceState&lt;/code&gt;. Manipulating the history will not cause the page to reload.&lt;/p&gt;

&lt;p&gt;There is a lot more you can do with the history API, but I hope this gave you a good intro.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;MDN docs on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/pushState" rel="noopener noreferrer"&gt;pushState&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState" rel="noopener noreferrer"&gt;replaceState&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;freeCodeCamp's &lt;a href="https://www.youtube.com/watch?v=C9vsQkMu5gk" rel="noopener noreferrer"&gt;tutorial on Browser history&lt;/a&gt; (8 minute youtube video)&lt;/li&gt;
&lt;/ul&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;API stands for &lt;em&gt;Application Programming Interface&lt;/em&gt;. The idea behind this is that, an application (in our case the browser) provides an interface for developers. We (developers) can use this interface to do cool things. Often times, a server which provides data via HTTP is also called API, which is not wrong but also does not do it justice. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;With this in mind, we could even say that &lt;code&gt;history.back()&lt;/code&gt; is actually the same as &lt;code&gt;history.go(-1)&lt;/code&gt;. The same is true for &lt;code&gt;history.forward()&lt;/code&gt;, which is actually the same as &lt;code&gt;history.go(1)&lt;/code&gt;.  ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;SPA is an acronym describing the idea of a &lt;em&gt;Single Page App&lt;/em&gt;. What is meant by this a departure from of the classic approach of having a user navigate from one page to another by making new HTTP calls to the server and receiving a new version of the page. SPAs go a different route in which they only load one page and include all the logic in this single page's Javascript. The Javascript will make intelligent decisions about what to display on the page and will send and load data from a server. There are many frameworks who can help building SPAs, some of which are: React.js, Angular.js and Vue.js. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>historyapi</category>
    </item>
    <item>
      <title>Difference between encoding, hashing and encryption</title>
      <dc:creator>Florian H.</dc:creator>
      <pubDate>Thu, 09 Apr 2020 13:03:04 +0000</pubDate>
      <link>https://dev.to/__florian/difference-between-encoding-hashing-and-encryption-54e4</link>
      <guid>https://dev.to/__florian/difference-between-encoding-hashing-and-encryption-54e4</guid>
      <description>&lt;p&gt;When I was starting out as a junior developer, it was very hard for me to tell some concepts apart, because I never took the time to sit down and learn about those. Helping with this journey is the intention of this article. I will simplify and shorten a few things here and there to make the topics more digestible and less hard to understand.&lt;/p&gt;

&lt;p&gt;This article will only give you an overview over which is which, so that you can dive deeper into the topics that interest you the most. 🙂&lt;/p&gt;

&lt;h2&gt;
  
  
  Encoding
&lt;/h2&gt;

&lt;p&gt;Encoding is all about representing some kind of information (let's say a word or a text) in a way that can be conveniently saved or transfered. A popular thing to do on computers is to write a little text (like this one) and save it onto a file storage (like my computer's harddisk). A file storage will only be able to save binary data (think: 1s and 0s), so I need to &lt;em&gt;encode&lt;/em&gt; my text in 1s and 0s.&lt;/p&gt;

&lt;p&gt;Let's say, I want to encode the German word for the color green in binary form, the result would look like this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JTIOPtWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/utf8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JTIOPtWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/utf8.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
Each letter will be transformer into a series of eight 1s or 0s. Why did I choose to use a German word? Because the German character "ü" does not fit into one set of eight 0s and 1s. This encoding is called &lt;strong&gt;UTF-8&lt;/strong&gt;. You probably have heard this before.&lt;/p&gt;

&lt;p&gt;Because using text on a computer is so important for people, people have come up with many ways to encode text, and UTF-8 is the one that is used the most because it can encode almost all language's characters (German, Swedish, Japanese, Chinese,…).&lt;/p&gt;

&lt;p&gt;Obviously, when you know that the binary data (1s and 0s) are in UTF-8, you can easily turn them back into your actual characters and display them on the screen again.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m6lJrp9a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/utf8-reverse.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m6lJrp9a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/utf8-reverse.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The binary data could also represent other kinds of information. A series of 0s and 1s could – for example – be numbers in a spreadsheet or colors in a picture of a puppy. Math (especially in school) mostly uses the decimal system. If our binary data represents data in the decimal system, our encoding would look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kXiFsX0r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/decimal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kXiFsX0r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/decimal.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are also other ways to encode data, some of which are base64, HEX or even ascii. Encoding always means the same thing:&lt;/p&gt;

&lt;p&gt;I have data and I want to represent it in another system. To achieve this, I encode my data in another system's rules. You don't loose or hide data, it is just in a different format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hashing
&lt;/h2&gt;

&lt;p&gt;For a long time, it has been considered best practices to only store the "hashed" version of a user's password in the database instead of the password itself. But what does hashing mean, and what does this have to do with passwords?&lt;/p&gt;

&lt;p&gt;Almost all programming languages come with a series of hashing functions (or you can add those functionality by installing libraries). You can provide some kind of input (like a word or short text) to them and they will return a "hashed" version of that input.&lt;/p&gt;

&lt;p&gt;One example of those is SHA512, which will return a 512 bit long hash.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V5c_FsFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/sha512.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V5c_FsFn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/sha512.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How is this different from Encoding? Hashing functions only work one-way: You can turn your input into a hash, but you cannot turn your hash into the input again. It is a one-way-street. You actually loose some of the information by hashing.&lt;/p&gt;

&lt;p&gt;The fact that this is a one-way-street is really great for storing passwords: You actually don't want to store the original password of the user, in case somethings goes sideways and an attacker steals your database.&lt;/p&gt;

&lt;p&gt;Hashing functions have differents "strengths": With growing computational power of computers, these functions usually become cracked after some years, which is why it is important to use an up-to-date one. Old hashing functions like &lt;code&gt;md5&lt;/code&gt; are considered insecure for years now, and the current (early 2020) favorite is &lt;code&gt;bcrypt&lt;/code&gt; (which actually uses a lot of clever ideas to defend itself from being cracked soon).&lt;/p&gt;

&lt;p&gt;So hashing is different from encoding, because it intentionally looses some of it's data to become a "one way street" function. Hashes cannot be turned back into the input that produced them, which is why they are great for storing passwords.&lt;/p&gt;

&lt;h2&gt;
  
  
  Encryption
&lt;/h2&gt;

&lt;p&gt;Keeping a secret a secret is one of the hardest and most valued things in human interaction and by that also in computing. What is definitely true for secret government files, is just as true for private conversations between coworkers or family members. You really don't want your parents to know that they will be getting a new computer as a holiday gift, which is why you want to encrypt your buying list.&lt;/p&gt;

&lt;p&gt;Encryption is a way of turning any input (like a word or a file) into some kind of data, which cannot be turned back into the input without knowing the secret of how to do this. A typical secret is a password: You use a password to encrypt your holiday shopping list and save it on your computer.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bIWEG9dP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/encrypt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bIWEG9dP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/encrypt.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Later, when you want to see the list again, you have to use the same password to see that list again.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iHWFe1Sm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/decrypt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iHWFe1Sm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://florianherlings.de/public/2020-04-05-encoding-hashing-encryption-differences/decrypt.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So in the simplest case encryption is a "two way street", which allows you to turn input and a password into an encrypted representation. This representation can later be turned back into the input, if you have the password. There are much more sophisticated encryption algorithms, but they all try to do the same thing: Come up with a representation of your data, that is not useful to anybody, if they don't have the password. Encryption does not loose any data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So what is the difference between all three? Encoding is just a different representation of your input. Hashing is a one-way-street to get a unrecoverable representation. And encryption is a safe way to store information that you want to look at again later.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Looses data?&lt;/th&gt;
&lt;th&gt;Readable by anybody&lt;/th&gt;
&lt;th&gt;Output can be turned back into input&lt;/th&gt;
&lt;th&gt;Use case&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Encoding&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Saving text into a file, sending data to a server, …&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hashing&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Storing passwords&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Encryption&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Saving secret files, sending secret messages or emails.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I hope this little article helped you to get a general overview of those topics. It is certainly true, that I made a few generalizations to break the topic down into a more digestible format. From here, you can make a deep dive into any of those topics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Selection of different encoders: &lt;a href="https://onlinebinarytools.com/convert-utf8-to-binary"&gt;onlinebinarytools.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Great base64 encoder: &lt;a href="https://www.base64encode.org/"&gt;base64encode.org&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Short article, explaining what is wrong about many hashing functions: &lt;a href="https://dusted.codes/sha-256-is-not-a-secure-password-hashing-algorithm"&gt;dusted.codes: "SHA-256 is not a secure password hashing algorithm"&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Wikipedia article about &lt;a href="https://en.wikipedia.org/wiki/Bcrypt"&gt;bcrypt&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(Header Photo by &lt;a href="https://unsplash.com/@herrherrmann"&gt;Sebastian Herrmann&lt;/a&gt; on Unsplash, this was first published on &lt;a href="https://florianherlings.de/posts/2020-04-07-encoding-hashing-encryption-differences/"&gt;my blog&lt;/a&gt;.)&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
