<?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: Brian Emilius</title>
    <description>The latest articles on DEV Community by Brian Emilius (@brianemilius).</description>
    <link>https://dev.to/brianemilius</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%2F51789%2Fed10d85d-5388-4bce-abf2-f6642b8c3b9d.png</url>
      <title>DEV Community: Brian Emilius</title>
      <link>https://dev.to/brianemilius</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianemilius"/>
    <language>en</language>
    <item>
      <title>The ExpressJS Router</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Wed, 18 Mar 2020 10:33:23 +0000</pubDate>
      <link>https://dev.to/brianemilius/the-expressjs-router-472b</link>
      <guid>https://dev.to/brianemilius/the-expressjs-router-472b</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="http://www.brianemilius.com/the-express-router"&gt;www.brianemilius.com/the-express-router&lt;/a&gt;.&lt;br&gt;
Visit &lt;a href="http://www.brianemilius.com"&gt;www.brianemilius.com&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;



&lt;p&gt;ExpressJS is probably the most popular framework for creating server-side web applications in Node.js.&lt;br&gt;
In this blog series I want to show you how to get started and how to do it The Right Way™.&lt;/p&gt;

&lt;p&gt;Articles in this series so far&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.brianemilius.com/get-started-with-expressjs/"&gt;Get Started With ExpressJS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brianemilius.com/expressjs-structure/"&gt;ExpressJS Project Structure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brianemilius.com/the-express-router/"&gt;The ExpressJS Router&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Upcomming articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ExpressJS Template Engines&lt;/li&gt;
&lt;li&gt;ExpressJS Middleware&lt;/li&gt;
&lt;li&gt;How To Parse Content From Forms With ExpressJS&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node.js and npm&lt;/li&gt;
&lt;li&gt;Some intermediate JavaScript knowledge&lt;/li&gt;
&lt;li&gt;Basic HTML and CSS&lt;/li&gt;
&lt;li&gt;It's a good idea to have read the previous posts in this series.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;I've written about the very basics of ExpressJS and how to build a solid project structure that scales. In this article we'll take a look at the Router and what we can do with it.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Even Is Routing?
&lt;/h2&gt;

&lt;p&gt;The basic idea of routing in web applications is to make sure a request is directed to the correct resource and the correct functionality.&lt;/p&gt;

&lt;p&gt;Any request to a web application is made with the &lt;a href="https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol"&gt;HTTP-protocol&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When a server receives a HTTP request from a client it looks at the &lt;code&gt;path&lt;/code&gt;, the &lt;code&gt;query&lt;/code&gt;, and the &lt;code&gt;fragment&lt;/code&gt; part of the request, as well as the HTTP method with which the request was made.&lt;/p&gt;

&lt;p&gt;The server then decides what to do with this request - how to treat it. This is what the router does.&lt;/p&gt;

&lt;p&gt;Once the router has delegated the request to the proper resource, the server sends a response back to the client.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I-Bogi4Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fbarn72xgzhbunhpf51h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I-Bogi4Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fbarn72xgzhbunhpf51h.png" alt="Request flow between client and server" width="241" height="181"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The Express Router
&lt;/h2&gt;

&lt;p&gt;This brings us to the router functionality in Express. A very basic router function could look something like this:&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;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ... treat the request&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to your profile&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// send a response&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break this down.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;router.get&lt;/code&gt; determines which &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods"&gt;HTTP method&lt;/a&gt; the router should capture. HTTP methods are a part of the HTTP specification and are meant to convey what a request wishes to do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;GET&lt;/code&gt; is for retrieving a resource. Every time you enter a URL in your browser and hit enter you effectively create a &lt;code&gt;GET&lt;/code&gt; request. This is the most common method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;POST&lt;/code&gt; is for creating a resource.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE&lt;/code&gt; is for deleting a resource.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PUT&lt;/code&gt; and &lt;code&gt;PATCH&lt;/code&gt; are for updating resources, but they do it differently from each other; &lt;code&gt;PUT&lt;/code&gt; replaces the resource to be updated entirely, while &lt;code&gt;PATCH&lt;/code&gt; updates only parts of the resource to be updated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You know two of these methods from HTML-forms.&lt;/p&gt;

&lt;p&gt;Next is &lt;code&gt;"/profile"&lt;/code&gt;. This is the HTTP request path, query, and fragment part. Here we tell the router which "resource" on the server the router should work for. There are several things we can do with this, which I will get into later in this article. The path basically means that we can type in &lt;code&gt;http://mydomain.com/profile&lt;/code&gt; in the browser, and this will trigger the above router function.&lt;/p&gt;

&lt;p&gt;Lastly, the callback function:&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;function&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ... code&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what is called a middleware function in the Express framework. It takes as a minimum 2 arguments; Request, and Response.&lt;/p&gt;

&lt;p&gt;Requests and responses always contain a header and a body. The header deals with meta-information about the request or response, and the body is the actual contents of the request or the response.&lt;/p&gt;

&lt;p&gt;The Request object contains all relevant information about the request, and the Response object the same, but for the response.&lt;/p&gt;

&lt;p&gt;You can &lt;code&gt;console.log&lt;/code&gt; the two to see exactly what they contain.&lt;/p&gt;

&lt;h2&gt;
  
  
  What We Can do With The Router
&lt;/h2&gt;

&lt;p&gt;The router function is a very flexible tool that lets you capture requests to the server, to server resources, and it can sort through the HTTP request methods used - as I explained above. But we can digest requests even further, looking at pre-defined parameters or query strings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parameters
&lt;/h3&gt;



&lt;div class="highlight js-code-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="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/product/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&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="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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You requested a product with the ID: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&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;params&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&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;A parameter is defined in the route resource string with a colon prefix &lt;code&gt;:id&lt;/code&gt;. You can reference it inside your router callback function in the request object.&lt;/p&gt;

&lt;p&gt;If there are any parameters defined, the request object will have an object named &lt;code&gt;params&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This means, that if you defined a parameter as &lt;code&gt;:id&lt;/code&gt;, you can access it with &lt;code&gt;request.params.id&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Query Strings
&lt;/h3&gt;

&lt;p&gt;Query strings are often used to add modifications to a resource request. A very common use-case is pagination:&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/products?page=2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&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="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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You requested to see page: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&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;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;page&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As with parameters, query strings are defined in the route resource string as you would a common query string &lt;code&gt;?page=1&lt;/code&gt;. If you need more query string variables you can separate them with a &amp;amp; &lt;code&gt;?page=1&amp;amp;limit=10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And, as with parameters, you can access the query string variables in the request object &lt;code&gt;request.query.page&lt;/code&gt; and &lt;code&gt;request.query.limit&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Is The Difference?
&lt;/h3&gt;

&lt;p&gt;Well, I would say parameters are used for making pretty URLs, such as &lt;code&gt;https://mywebsite.com/products/42&lt;/code&gt;, and query string variables should be used for search strings, and modifications such as page number, limits, language localization, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  RegExp
&lt;/h3&gt;

&lt;p&gt;You can even define your route resource string as a regular expression!&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/a/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// treat the request ...&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This route captures all resources that has an 'a' in it.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Middleware
&lt;/h2&gt;

&lt;p&gt;Remember how I said that the router callback function is what we call a middleware function in Express?&lt;/p&gt;

&lt;p&gt;A middleware function is basically a function that can access the request and response object. But there is one more, which is important to know about: &lt;code&gt;next&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A true Express middleware function has all 3 objects as parameters:&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;function&lt;/span&gt; &lt;span class="nx"&gt;myMiddlewareFunction&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// treat the request ...&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="c1"&gt;// provide either a&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// or a &lt;/span&gt;
    &lt;span class="nx"&gt;next&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;The &lt;code&gt;response.send()&lt;/code&gt; method sends headers and a response body to the client. This basically ends the response, and no more headers can be sent after this.&lt;/p&gt;

&lt;p&gt;But if we instead use the &lt;code&gt;next()&lt;/code&gt; callback function, we send the request on to the &lt;em&gt;next&lt;/em&gt; middleware function down the line.&lt;/p&gt;

&lt;p&gt;What does this mean? Well, you can, in fact, have as many middleware functions in a route as you wish.&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;function&lt;/span&gt; &lt;span class="nx"&gt;myFirstMiddleware&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&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;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authorization&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bearer 123456789&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&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;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;401&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;You are not allowed to view this page.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;mySecondMiddleware&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="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, and welcome to the secret page.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/secret&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;myFirstMiddleware&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mySecondMiddleware&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, two middleware functions are defined. The first one makes a check on the request header to see if it includes an authorization header with a correct Bearer token. If it does not, the middleware replies with status code 401 (unauthorized) and a message.&lt;/p&gt;

&lt;p&gt;Otherwise, the first middleware sends the request on to the next middleware, which shows a secret page.&lt;/p&gt;

&lt;p&gt;You can write your own middleware functions like this - but there is also a vast library of middleware functions on open source platforms such as npm.&lt;/p&gt;

&lt;p&gt;Read more about the Express Router on the &lt;a href="https://expressjs.com/en/guide/routing.html"&gt;Express website documentation&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Cover image by &lt;a href="https://www.freepik.com/free-photos-vectors/design"&gt;www.freepik.com&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>serverside</category>
      <category>express</category>
    </item>
    <item>
      <title>ExpressJS Project Structure</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Wed, 11 Dec 2019 08:33:37 +0000</pubDate>
      <link>https://dev.to/brianemilius/expressjs-project-structure-2ka4</link>
      <guid>https://dev.to/brianemilius/expressjs-project-structure-2ka4</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="http://www.brianemilius.com/expressjs-structure"&gt;www.brianemilius.com/expressjs-structure&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;ExpressJS is probably the most popular framework for creating server-side web applications in Node.js.&lt;br&gt;
In this blog series I want to show you how to get started and how to do it The Right Way™.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node.js and npm&lt;/li&gt;
&lt;li&gt;Some intermediate JavaScript knowledge&lt;/li&gt;
&lt;li&gt;Basic HTML and CSS&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;In the previous blog post I introduced you to ExpressJS with a very simple example code.&lt;/p&gt;

&lt;p&gt;For this post I want to show you how to build a proper project structure for your ExpressJS application, that will scale and is robust.&lt;/p&gt;

&lt;p&gt;The keyword for what I want to do here is "fragmentation". We are going to divide the application into a lot of small, modular files.&lt;/p&gt;

&lt;p&gt;The reasoning behind this choice is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;smaller files are easier to maintain&lt;/li&gt;
&lt;li&gt;collaboration runs more smoothly on version control platforms such as GitHub&lt;/li&gt;
&lt;li&gt;you can inject or remove functionality without breaking the application in other places&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  The folder structure
&lt;/h2&gt;

&lt;p&gt;Let's start out by taking a look at the folder structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-express-app/
|   app.js
|   package.json
|
└───node_modules/
|   |   ...
|
└───bin/
|   |   www
|
└───config/
|   |   parsing.js
|   |   view-engine.js
|   |   static-files.js
|   |   database.js
|
└───controllers/
|   |   page.controller.js
|
└───models/
|   |   page.model.js
|
└───views/
|   |   page.ejs
|   |
|   └───partials/
|       |   head.ejs
|       |   seo.ejs
|       |   header.ejs
|       |   footer.ejs
|       |   after-footer.ejs
|
└───routes/
|   |   page.route.js
|   |   router.js
|
assets/
    |   ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Model, View, Controller
&lt;/h3&gt;

&lt;p&gt;The folders &lt;code&gt;models&lt;/code&gt;, &lt;code&gt;views&lt;/code&gt;, and &lt;code&gt;controllers&lt;/code&gt; are at the very core of the application functionality and is meant to use the programming design pattern "MVC".&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;

&lt;p&gt;In the folder &lt;code&gt;config&lt;/code&gt; we will keep all the little configuration files that express makes use of. Anything from database setup, which parser to use, or what view engine the application should use.&lt;/p&gt;

&lt;h3&gt;
  
  
  The router
&lt;/h3&gt;

&lt;p&gt;I've taken routing logic completely out of the main application file and placed it by itself. As I mention in the previous blog post, I like to keep application configuration and logic separate from the router engine. Basically, the router engine should be able to function on its own. This is a great pattern for testing purposes, in my opinion.&lt;/p&gt;

&lt;h3&gt;
  
  
  www and app.js
&lt;/h3&gt;

&lt;p&gt;The app.js file is the main application file. Here we gather all the threads from the configuration and router. This file is also meant to handle application errors and provide a flow for error pages (for example a 404 page) and error reporting (for example logging).&lt;/p&gt;

&lt;p&gt;The www file is the application executable. This file starts the server and handles any server-related errors. We can have several different executables for different situations. An example could be one executable file for development and one for the production environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static files
&lt;/h3&gt;

&lt;p&gt;Finally, most websites have a bunch of static files that needs to be served so they can be viewed in the browser. These are files such as stylesheets, front-end related JavaScript, images and other media files. These files are kept in the &lt;code&gt;assets&lt;/code&gt; folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tie the fragments together
&lt;/h2&gt;

&lt;p&gt;First, let's create the main application file &lt;code&gt;app.js&lt;/code&gt; with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Here, we will require the configuration files&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it, really. For now. We still need to add requirements for some of our configuration files and the router, but all that comes later. This is the very least we need for the application to run.&lt;/p&gt;

&lt;p&gt;The next file we want to create is the &lt;code&gt;bin/www&lt;/code&gt; executable. There are 3 things we need to do in this file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;require the application&lt;/li&gt;
&lt;li&gt;create a server instance and make it listen&lt;/li&gt;
&lt;li&gt;handle errors related to the server instance
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// bin/www&lt;/span&gt;&lt;span class="cp"&gt;

#!/usr/bin/env node
&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Define a port for the server to listen on&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;port&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Create a server instance&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Make the server listen on a port&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Handle errors and success&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listening&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onListening&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;pipeOrPort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`pipe &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;syscall&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listen&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pipeOrPort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EACCES&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; requires elevated privileges.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EADDRINUSE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is already in use.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;error&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;onListening&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pipeOrPort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Listening on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start out by requiring the &lt;code&gt;app&lt;/code&gt; module from app.js as well as the http package, which is a part of the Node.js general API.&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;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http&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;Next, we define a port for the server to listen on. In this case, the port is set to whatever is in our environment variable &lt;code&gt;PORT&lt;/code&gt; or, if the environment variable doesn't exist; 3000.&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;let&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;port&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we create a server instance and make it listen on the port.&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;let&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The rest of the file is basic event handling. We set up listeners for 2 events: "error" and "listening".&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;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listening&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onListening&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the error event is triggered, we call the function &lt;code&gt;onError&lt;/code&gt; which handles a few different scenarios, such as &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the server cannot run because the port we defined is in use by another program&lt;/li&gt;
&lt;li&gt;the server cannot run because the operating system requires certain privileges for a user to run a program&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the listening event is triggered, we log a short message to the console to let us know the server is running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuration injection
&lt;/h2&gt;

&lt;p&gt;Now that we've got the application and server set up and it's basically ready to run, I want to show you how you can tie in configuration modules. We will be making a single configuration module:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;config for the location of our static files folder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need two files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// config/static-files.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/assets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;static&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;assets&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration is a middleware function. It tells express to use a function, &lt;code&gt;express.static("assets")&lt;/code&gt;, on requests to the "&lt;a href="http://localhost:3000/assets"&gt;http://localhost:3000/assets&lt;/a&gt;" address and serve the folder "assets" as static content to the client.&lt;/p&gt;

&lt;p&gt;Basically, this means that when a client navigates to "&lt;a href="http://localhost:3000/assets"&gt;http://localhost:3000/assets&lt;/a&gt;" they have access to anything inside the "assets" folder.&lt;/p&gt;

&lt;p&gt;I will dive deeper into what middlewares are in a later article.&lt;/p&gt;

&lt;p&gt;Finally, inject this module to our app.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Here, we will require the configuration files&lt;/span&gt;
&lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./config/static-files&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Test all the things!
&lt;/h2&gt;

&lt;p&gt;Now it's time to see if all we did actually work. To test the server, I want to have a HTML file in the assets folder, that prints out a simple message, so I can see that everything runs correctly so far.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- assets/test.html --&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Hello there!&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;If you can see this, the server is running and has been configured correctly.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now save the file and run the server from your console with the command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node bin/www
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the message&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Listening on port 3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then open your browser and navigate to &lt;code&gt;http://localhost:3000/assets/test.html&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You should see this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zxuirJ55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y5rirhpclz32c5yo7coa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zxuirJ55--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/y5rirhpclz32c5yo7coa.png" alt="Browser window with a demo message" width="671" height="494"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have followed along so far, I would really love your input. Are the concepts I demonstrate easy to understand or is there something you wish I explain more clearly?&lt;/p&gt;

&lt;p&gt;I have prepared &lt;a href="https://github.com/brians-open-source-stuff/express-tutorial"&gt;a small repository on GitHub&lt;/a&gt;, that you can clone and play around with. It contains everything covered in this post.&lt;/p&gt;

&lt;p&gt;The next article will be about the router.&lt;/p&gt;

&lt;p&gt;Articles in this series so far&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.brianemilius.com/get-started-with-expressjs/"&gt;Get Started With ExpressJS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.brianemilius.com/expressjs-structure/"&gt;ExpressJS Project Structure&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Upcoming articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The ExpressJS Router&lt;/li&gt;
&lt;li&gt;ExpressJS Template Engines&lt;/li&gt;
&lt;li&gt;ExpressJS Middleware&lt;/li&gt;
&lt;li&gt;How To Parse Content From Forms With ExpressJS&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/people"&gt;Construction photo created by jcomp - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>serverside</category>
      <category>express</category>
    </item>
    <item>
      <title>Get Started With ExpressJS</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Tue, 10 Dec 2019 08:51:32 +0000</pubDate>
      <link>https://dev.to/brianemilius/get-started-with-expressjs-4fm6</link>
      <guid>https://dev.to/brianemilius/get-started-with-expressjs-4fm6</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was first published on &lt;a href="http://www.brianemilius.com/get-started-with-expressjs/"&gt;www.brianemilius.com/get-started-with-expressjs/&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;ExpressJS is probably the most popular framework for creating server-side web applications in Node.js.&lt;br&gt;
In this blog series I want to show you how to get started and how to do it The Right Way™.&lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of Node.js and npm&lt;/li&gt;
&lt;li&gt;Some intermediate JavaScript knowledge&lt;/li&gt;
&lt;li&gt;Basic HTML and CSS&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;First things first. Before I throw you into a lot of little code snippets and techy-talk, let's have a look at what is a server-side web application.&lt;/p&gt;

&lt;p&gt;Basically, we have two kinds of web applications that differ majorly: Server-side and Client-side.&lt;/p&gt;

&lt;p&gt;On the client-side, the usual case is, that a browser receives a scaffold HTML page from the server. It then queries the server for data and then renders the data into the HTML scaffold. We call this client-side-rendering (CSR for short).&lt;/p&gt;

&lt;p&gt;This use-case is very popular (in 2019) and you see it on a lot of big company websites such as Facebook or Youtube.&lt;/p&gt;

&lt;p&gt;Two of the reasons for the popularity of this method are performance and maintenance. Servers perform better, if they have to do less work with every request. This also means that maintaining a server environment is a lot easier (and, importantly, a lot cheaper).&lt;/p&gt;

&lt;p&gt;The downside of methods like this is that we put a lot of the work on client devices. This means that phones or laptops drain their battery faster, and data plans gets used up quicker.&lt;/p&gt;

&lt;p&gt;On the flip-side is server-side-rendering (SSR for short).&lt;/p&gt;

&lt;p&gt;SSR is a method, where the server receives a request for a webpage. It then gathers all the data it needs to put together a HTML-page, renders the page, and then ships it off to the client.&lt;/p&gt;

&lt;p&gt;This method leaves almost all the hard work to the server, and not the client device. The downside here is that the server actually has to do some work for every request. The more requests your web-application gets, the harder the server has to work.&lt;/p&gt;
&lt;h2&gt;
  
  
  Now let's dive in
&lt;/h2&gt;

&lt;p&gt;To begin with we need to initialize an NPM project and install the ExpressJS package. Create a new folder somewhere on your computer. This folder is our root.&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;mkdir &lt;/span&gt;my-express-app
&lt;span class="nb"&gt;cd &lt;/span&gt;my-express-app
npm init &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; npm i express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's create an app.js file in the root folder and enter this code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.js&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;routerCallback&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="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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1337&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is listening on port 1337&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Standard configuration
&lt;/h3&gt;

&lt;p&gt;First, we require the express library. This is needed for the entire application to function at all.&lt;/p&gt;

&lt;p&gt;Then we invoke the express library and put it in a variable named &lt;code&gt;app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also need to invoke the &lt;code&gt;Router&lt;/code&gt; function from express.&lt;/p&gt;

&lt;p&gt;These 3 items are the core of the application. We could choose not to use the Router function, but in this example I am using it because I like to separate application configuration and logic from the router engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Routing
&lt;/h3&gt;

&lt;p&gt;The next thing in the application is the route. We create a listener for the endpoint "/", which is the root of our homepage (the "frontpage").&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;Router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;routerCallback&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="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;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, World!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The listener listens for any requests to the endpoint with the HTTP method GET. If a client makes a GET request to the endpoint address, the callback function is triggered. I've put a very short example of how to make something appear on your website. This one renders ye olde "Hello, World!" message.&lt;/p&gt;

&lt;p&gt;Once we've written all our route functions (yes, we can have as many as we want), we need to tell the application to use the Router.&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Starting the server
&lt;/h3&gt;

&lt;p&gt;Lastly we start the server by telling our app to listen on a port. In this example I use port 1337 but in reality you could use any non-reserved port. A common port to use with express applications is 3000.&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1337&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is listening on port 1337&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Start the application and test it
&lt;/h2&gt;

&lt;p&gt;Once we've saved the &lt;code&gt;app.js&lt;/code&gt; file we can run the application and test it. To run it, type this in your console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should give you the message&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Server is listening on port 1337
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can open the website in a browser by navigating to this address: &lt;a href="http://localhost:1337/"&gt;http://localhost:1337/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is what you should see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2MYR5lN1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/f16qncnxir8ei8g12hhl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2MYR5lN1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/f16qncnxir8ei8g12hhl.png" alt='Browser window with the text "Hello, World!"' width="671" height="494"&gt;&lt;/a&gt;&lt;br&gt;
To stop the application, click &lt;code&gt;CTRL&lt;/code&gt;+&lt;code&gt;C&lt;/code&gt; in your console.&lt;/p&gt;

&lt;p&gt;This is the very basics of getting started with ExpressJS. In the next blog-post I will write a bit about the structure of an express application.&lt;/p&gt;

&lt;p&gt;Let me know in the comments if you followed along and how it worked out for you.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/technology"&gt;Technology vector image created by slidesgo - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>serverside</category>
      <category>express</category>
    </item>
    <item>
      <title>Get Unstuck!</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Mon, 02 Sep 2019 11:03:14 +0000</pubDate>
      <link>https://dev.to/brianemilius/get-unstuck-1m1j</link>
      <guid>https://dev.to/brianemilius/get-unstuck-1m1j</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="http://www.brianemilius.com/get-unstuck"&gt;www.brianemilius.com/get-unstuck&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Some code in my application is being naughty and won't do as I tell it. And I just cannot fathom what could possibly be wrong! I've had this issue for days now. No matter how I look at it, I just can't see a way out. I'm a stubborn fellow, so I dig in deep and keep gnawing at this problem until I alone emerge victoriously.&lt;/p&gt;

&lt;p&gt;We've all tried this. We've all had an issue that we struggled to solve. We all probably both hate and love these situations - I know I do! Problem-solving is at the very core of what it means to be a software developer.&lt;/p&gt;

&lt;p&gt;So how do we do it? How do we time and again emerge triumphantly? I don't know how you do it, but I know what I do. This article is about that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cannot read property 'xyz' of undefined
&lt;/h2&gt;

&lt;p&gt;First of all, I've put in some time learning the different kinds of error types my language has. For example, it pays off for me to know the difference between "cannot read x of undefined" and "unhandled exception".&lt;/p&gt;

&lt;p&gt;I've taught myself to analyse the error messages I get in the console, search through my code where the error message suggests and look for something related to the error type.&lt;/p&gt;

&lt;p&gt;An example!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getUserByUsername(req, res, next) {
  try {
    const [result] = await conn.query("SELECT id, name FROM users WHERE username = :username", {
      username: req.fields.username
    });
    res.json(JSON.parse(result));
    return;
  }
  catch (error) {
    next(error);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is a middleware for something like Express or Restify. If I got the error&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Cannot read 'username' of undefined on /path/to/file:4:28&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I know what file to look in and what line to look at. But I also know that 'username' cannot be read because of the thingy 'username' is stuck on (fields) is undefined. Now I can begin to detective my way out of the problem - by asking: "Why is 'fields' undefined? What did I forget to do?"&lt;/p&gt;

&lt;p&gt;Learning what the different kinds of error types mean has probably been the most important thing for me when it comes to debugging my code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Console.log(all the things)
&lt;/h2&gt;

&lt;p&gt;I've got a bunch of nested conditionals in my code and I am not getting the response I expected.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log("here")&lt;/code&gt; to the rescue!&lt;/p&gt;

&lt;p&gt;I use this little debugging trick so much you might say it's my go-to solution. Move the &lt;code&gt;console.log()&lt;/code&gt; around in your code. Notice where it pops up and where it doesn't.&lt;/p&gt;

&lt;p&gt;It might seem dumb, but you'd be surprised how often this simple little trick helps (how's that for a click-batey title?).&lt;/p&gt;

&lt;p&gt;"But doesn't the IDE debugger already give you this functionality?" you might ask.&lt;/p&gt;

&lt;p&gt;Well, yes. It does. Often, spinning up the debugger just to know where my logic fails in a JavaScript is a bit much, though.&lt;/p&gt;

&lt;p&gt;That being said:&lt;/p&gt;

&lt;h2&gt;
  
  
  Befriend Your Debugging Tool
&lt;/h2&gt;

&lt;p&gt;All the big IDEs and VS Code have debugging tools. How you use them differs a bit from IDE to IDE. But get to know yours. Learn about break-points, the call stack, jump in and jump over, and so on.&lt;/p&gt;

&lt;p&gt;I often run an application in debugging mode when I develop. This lets me add break-points on the fly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Take A Break!
&lt;/h2&gt;

&lt;p&gt;For real! Do something else. Go for a walk, clear your mind. Work on a different problem in a different language. Help someone else with their coding problem. Whatever you do, do something that will distract you and take your mind off of your problem.&lt;/p&gt;

&lt;p&gt;Some of you might think this is some hippie new-age advice. But it helps. I promise! There's even &lt;a href="https://www.sciencedaily.com/releases/2011/02/110208131529.htm"&gt;science to back this claim&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quack!
&lt;/h2&gt;

&lt;p&gt;Rubber duck debugging is something I practice almost religiously. It's a method where you verbalise your issue. Preferably you do this to a rubber duck. The theory behind this practice is that when you have to describe a problem, the solution often presents itself within the description. I do this a lot with my students. So much, in fact, that some of my students refer to it as "being rubber ducked by Brian".&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn To Search
&lt;/h2&gt;

&lt;p&gt;Let's be honest. Googling the solution to a problem is something we all do. Or maybe we DuckDuckGo it. Maybe we only search on specific platforms such as StackOverflow. Whatever the case is for you, learn to formulate the core of your problem. Use keywords. Computers understand unhinged sentences such as "undefined error req.fields express" (bonus: This very search phrase on DuckDuckGo has a body-parser issue listed as the number 1 result, which, incidentally, is what is wrong in my example).&lt;/p&gt;

&lt;h2&gt;
  
  
  Learn To Formulate The Problem
&lt;/h2&gt;

&lt;p&gt;Asking a question on StackOverflow or similar communities can be a really great exercise. Why?&lt;/p&gt;

&lt;p&gt;You train yourself in being clear&lt;br&gt;
You might help others with similar problems along the way&lt;br&gt;
You might realise the solution as you describe your issue (rubber duck debugging gone digital).&lt;/p&gt;

&lt;h2&gt;
  
  
  Ask
&lt;/h2&gt;

&lt;p&gt;Seriously. There's no shame in asking for help. Know what you know. But more importantly; know when there is something you don't know. Use it as an opportunity to learn something new. Ask your friend, teacher, co-worker, the Almighty Google Overlord.&lt;/p&gt;

&lt;p&gt;Practice pair-programming. My experience is that programming in pairs gives cause for great discussions about the 'why' in your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't Worry
&lt;/h2&gt;

&lt;p&gt;Programming is hard. Programming is frustrating. Discipline your mind to understand that failure is a chance to learn. We don't learn anything from being good at the same thing over and over again. Dare to "move fast and break things" - just, like, not in a "let's create an app that has the potential to overthrow governments"-way.&lt;/p&gt;

&lt;p&gt;We all struggle. Daily even. It's okay, don't worry about it. It won't get any easier, but it will get a lot more fun as you learn and become a better developer.&lt;/p&gt;

&lt;p&gt;Now it's time for you to share your little tips and tricks for getting unstuck on a programming problem. Do you do any of the things I do?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/water"&gt;Cover image created by asier_relampagoestudio - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>What In Tarnation Are JavaScript Prototypes?</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Wed, 14 Aug 2019 16:12:27 +0000</pubDate>
      <link>https://dev.to/brianemilius/what-in-tarnation-are-javascript-prototypes-3800</link>
      <guid>https://dev.to/brianemilius/what-in-tarnation-are-javascript-prototypes-3800</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted on &lt;a href="http://www.brianemilius.com/javascript-prototypes"&gt;www.brianemilius.com/javascript-prototypes&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;A friend of mine asked me the other day about the MDN JavaScript manual.&lt;/p&gt;

&lt;p&gt;He noticed something odd when he looked up a function. For example, the title on the page for "forEach" was not "forEach". It was "Array.prototype.forEach".&lt;/p&gt;

&lt;p&gt;My friend wanted to know what that prototype thing was all about. This article is about that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Primitives
&lt;/h2&gt;

&lt;p&gt;So in JavaScript, we have something called "primitives". &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive"&gt;According to MDN&lt;/a&gt;, a primitive is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;data that is not an object and has no methods. There are 7 primitive data types: string, number, bigint, boolean, null, undefined, and symbol.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But each primitive, except for null and undefined, has something we call a "primitive wrapper object". These are the ones we want to look at.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; is the wrapper object for the primitive &lt;code&gt;string&lt;/code&gt;,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Number&lt;/code&gt; for the &lt;code&gt;number&lt;/code&gt; primitive,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;BigInt&lt;/code&gt; for the &lt;code&gt;bigint&lt;/code&gt; primitive,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Boolean&lt;/code&gt; for the &lt;code&gt;boolean&lt;/code&gt; primitive,&lt;/li&gt;
&lt;li&gt;and finally, &lt;code&gt;Symbol&lt;/code&gt; for the &lt;code&gt;symbol&lt;/code&gt; primitive.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice that the wrapper objects start with a capital letter (this is important).&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;All objects in JavaScript have a prototype. This includes objects that are not primitive wrappers, such as "Function", "Array", and "Object".&lt;/p&gt;

&lt;p&gt;The prototype of an object is just what you think it is; A prototype for the object.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prototypes
&lt;/h2&gt;

&lt;p&gt;Let us take a step back and clear up what a prototype is.&lt;/p&gt;

&lt;p&gt;Imagine we have a primitive in JavaScript called "human". The wrapper object would be named "Human".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1CUr391u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/f4l5igi2a8xltsrq2jm2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1CUr391u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/f4l5igi2a8xltsrq2jm2.jpg" alt='A smiling man. The word "Bob" and an arrow that points to the man.' width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The prototype of a Human is filled with properties and methods. For example, a few of the properties of a Human could be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eyes: 2&lt;/li&gt;
&lt;li&gt;Ears: 2&lt;/li&gt;
&lt;li&gt;Noses: 1&lt;/li&gt;
&lt;li&gt;Mouths: 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are properties that tell us what the Human has - what it is created with and looks like.&lt;/p&gt;

&lt;p&gt;The Human prototype also includes methods. These tell us what the Human can do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Eat&lt;/li&gt;
&lt;li&gt;Sleep&lt;/li&gt;
&lt;li&gt;Rave&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now imagine this piece of code in JavaScript that won't actually work in real life:&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;var&lt;/span&gt; &lt;span class="nx"&gt;Bob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Human&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create a variable, &lt;code&gt;Bob&lt;/code&gt;, which is built upon the Human prototype.&lt;/p&gt;

&lt;p&gt;We can now call any of the properties that Human has on Bob:&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;Bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Eyes&lt;/span&gt; &lt;span class="c1"&gt;// output: 2&lt;/span&gt;
&lt;span class="nx"&gt;Bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Mouths&lt;/span&gt; &lt;span class="c1"&gt;// output: 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also run any method the Human prototype has on Bob:&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;Bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// output: Bob probably eats something&lt;/span&gt;
&lt;span class="nx"&gt;Bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// output: Bob sleeps&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Back To Actual JavaScript
&lt;/h3&gt;

&lt;p&gt;As with Bob the Human, so it is with primitive wrapper objects in JavaScript.&lt;/p&gt;

&lt;p&gt;Let us use &lt;code&gt;Number&lt;/code&gt; as an example.&lt;/p&gt;

&lt;p&gt;The wrapper object &lt;code&gt;Number&lt;/code&gt; prototype has a bunch of methods we can run on any actual &lt;code&gt;number&lt;/code&gt; primitive. One I use often is "Number.prototype.toString".&lt;/p&gt;

&lt;p&gt;This method converts the number primitive into a string primitive.&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;var&lt;/span&gt; &lt;span class="nx"&gt;myNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: 42 &amp;lt;-- a number&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;myString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// output: "42" &amp;lt;-- a string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Got It! What Now?
&lt;/h2&gt;

&lt;p&gt;There are loads of useful methods you can run on objects. &lt;a href="https://developer.mozilla.org/en-US/docs/Web/javascript"&gt;The MDN manual&lt;/a&gt; has them all listed and there are WAY too many to name them here, so go explore!&lt;/p&gt;

&lt;p&gt;If you use &lt;code&gt;console.log()&lt;/code&gt; on a primitive wrapper object you can see the entire list of prototype properties and methods in your browser or console. That's a fun place to start.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/technology"&gt;Cover photo created by kjpargeter - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/people"&gt;Bob photo created by freepik - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>What to put in your portfolio as a beginner web dev</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Tue, 06 Aug 2019 13:35:10 +0000</pubDate>
      <link>https://dev.to/brianemilius/what-to-put-in-your-portfolio-as-a-beginner-web-dev-11je</link>
      <guid>https://dev.to/brianemilius/what-to-put-in-your-portfolio-as-a-beginner-web-dev-11je</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally published on &lt;a href="http://www.brianemilius.com/portfolio-ideas"&gt;www.brianemilius.com/portfolio-ideas&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog/"&gt;www.brianemilius.com/blog/&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;Last week I wrote up a list of advice for beginner web developers. One of the bullets on the list was to build a portfolio.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="/brianemilius" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aRN6XF52--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/practicaldev/image/fetch/s--f5yJ938t--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/51789/ed10d85d-5388-4bce-abf2-f6642b8c3b9d.png" alt="brianemilius"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="/brianemilius/my-advice-to-beginner-devs-4fo2" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;My Advice to Beginner Devs&lt;/h2&gt;
      &lt;h3&gt;Brian Emilius ・ Aug 3 '19 ・ 3 min read&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#beginners&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#career&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;But the challenge with this is that beginners have nothing to show.&lt;/p&gt;

&lt;p&gt;Or do they? Here is a list of ideas for beginner portfolios. Please share yours too at the bottom.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code to your portfolio itself. Put it up on GitHub and share the link.&lt;/li&gt;
&lt;li&gt;Finish a pet project and display it.&lt;/li&gt;
&lt;li&gt;Design a contact form complete with validation, error messages, and confirmation message.&lt;/li&gt;
&lt;li&gt;How about the same as above, but for a login/signup form?&lt;/li&gt;
&lt;li&gt;Or a newsletter signup/cancellation flow?&lt;/li&gt;
&lt;li&gt;A small site that gathers news from RSS feeds and displays them.&lt;/li&gt;
&lt;li&gt;Photo gallery&lt;/li&gt;
&lt;li&gt;Are you a member of a sports club or do you play in a band? Make a website for that - even if your club doesn't need one.&lt;/li&gt;
&lt;li&gt;Call up non-profits in your area and ask if they would like to make use of your skills (this is also a great exercise in making cold-calls).&lt;/li&gt;
&lt;li&gt;Make a few [insert your preferred CMS] templates.&lt;/li&gt;
&lt;li&gt;A case study of that time you helped your aunt make a genealogy site.&lt;/li&gt;
&lt;li&gt;Or a case study of that other time you helped Non-Profit X tune their SEO and accessibility issues.&lt;/li&gt;
&lt;li&gt;A responsive website navigation (that doesn't default to a hamburger menu) (Why? Because this requires some outside-the-box thinking).&lt;/li&gt;
&lt;li&gt;Contribute to an open-source project. Yes, this is a daunting task for pretty much all beginners, but a great place to start in the open-source community is documentation and translation (fix those comma errors).&lt;/li&gt;
&lt;li&gt;A Tic-Tac-Toe game app, but with your personal twist.&lt;/li&gt;
&lt;li&gt;A To-do app you can speak to.&lt;/li&gt;
&lt;li&gt;An analogue clock.&lt;/li&gt;
&lt;li&gt;A small chat interface, maybe for something like webshop live support.&lt;/li&gt;
&lt;li&gt;Set up a landing page for Relay For Life, even though they never asked you to (seriously, help spread the brand!).&lt;/li&gt;
&lt;li&gt;Share unfinished projects. This can say a lot about your thought process and has real value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's 20 ideas for your beginner portfolio. A good one has 6 to 10 examples that show off your skills. Remember; Show me, don't tell me. Use visual examples. Show me what you can do rather than talk about it.&lt;/p&gt;

&lt;p&gt;What ideas do you have? Or maybe you want to show off your portfolio so others can be inspired. Share in the comments.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>discuss</category>
    </item>
    <item>
      <title>My Advice to Beginner Devs</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Sat, 03 Aug 2019 10:02:24 +0000</pubDate>
      <link>https://dev.to/brianemilius/my-advice-to-beginner-devs-4fo2</link>
      <guid>https://dev.to/brianemilius/my-advice-to-beginner-devs-4fo2</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted on &lt;a href="http://www.brianemilius.com/advice-to-beginner-devs"&gt;www.brianemilius.com/advice-to-beginner-devs&lt;/a&gt;. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;I have been messing around on the World Wide Web since it was a thing. Today I teach Web Developer classes at college. Here is my advice for beginning web developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Doesn't matter how you did it
&lt;/h2&gt;

&lt;p&gt;Don't spend too much time fussing over your solution to a problem. If it works, you did it right.&lt;/p&gt;

&lt;p&gt;It doesn't really matter how you made your solution work. What matters is that it does work. In time you will learn to identify smarter or faster solutions. Or how to follow best-practice for your language or framework.&lt;/p&gt;

&lt;p&gt;But for now, make it work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Have a pet project
&lt;/h2&gt;

&lt;p&gt;When you learn new things it can be a tremendous benefit to test your knowledge on a project that you care about.&lt;/p&gt;

&lt;p&gt;The Internet is full of Hello, World! and To-do app examples. These are all great and will teach you the basics of what you can do with a language or tool.&lt;/p&gt;

&lt;p&gt;You have to apply your knowledge to something you are passionate about. This will help you internalise new learning. And it's a way to move past tutorials. Pet projects are perfect for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build a portfolio
&lt;/h2&gt;

&lt;p&gt;Yesterday is a great time to get started building your portfolio. A good portfolio should show your skills.&lt;br&gt;
Don't have any real-life cases to display? Don't worry; make some up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Finish that pet project of yours and put it in the portfolio.&lt;/li&gt;
&lt;li&gt;Display a few of your tutorial apps (but not too many).&lt;/li&gt;
&lt;li&gt;Call up a local sports club and ask if they need some free web-work done.&lt;/li&gt;
&lt;li&gt;Offer free SEO or accessibility audits to some local non-profit organisations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, I know it's a bit of a slippery slope to offer your work for free. I am not suggesting you should "trade hours for exposure". But these few free jobs can get your portfolio up and running. You will earn some real-world experience which can give you real leads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Even senior devs struggle
&lt;/h2&gt;

&lt;p&gt;Part of being a developer, any kind of program development, is problem-solving. You might say we make a living off finding solutions to problems.&lt;/p&gt;

&lt;p&gt;Problems can be... well, problematic. I've been doing this for 25 years. Sometimes I think web developers are a masochistic breed.&lt;/p&gt;

&lt;p&gt;I can get so frustrated over a problem that I am ready to quit. But over the years I have learned several problem-solving tricks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Break your problem into smaller parts.&lt;/li&gt;
&lt;li&gt;Learn to use debugging tools.&lt;/li&gt;
&lt;li&gt;Ask for help (for real!).&lt;/li&gt;
&lt;li&gt;Use pseudo-code.&lt;/li&gt;
&lt;li&gt;Use flow-charts.&lt;/li&gt;
&lt;li&gt;Go for a walk to clear your head.&lt;/li&gt;
&lt;li&gt;Work on something else for a while.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Keep up-to-date
&lt;/h2&gt;

&lt;p&gt;Web development is an industry that works both super fast and super slow. So slow it sometimes seems like you are watching paint dry.&lt;/p&gt;

&lt;p&gt;What do I mean by this? The World Wide Web has existed for over 30 years now. A lot has happened, but then again, we still use HTML, CSS, and JavaScript for a lot of it. This is the slow-evolving core (and this is a good thing!).&lt;/p&gt;

&lt;p&gt;Then there are server-side languages, frameworks, libraries, design systems and so on. These evolve at a break-neck pace. New things emerge almost every day. Some of these things grow to be common-place tools, others die.&lt;/p&gt;

&lt;p&gt;But keep up with the industry. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Listen to podcasts.&lt;/li&gt;
&lt;li&gt;Subscribe to a few mailing lists and newsletters.&lt;/li&gt;
&lt;li&gt;Follow industry leaders and first-movers on social media.&lt;/li&gt;
&lt;li&gt;Be curious.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Know your motivation
&lt;/h2&gt;

&lt;p&gt;It doesn't matter why you are a web developer. It pays your rent or it is your life's passion to make awesome web apps, build and be creative. Whatever your motivation is, don't lose sight of it.&lt;/p&gt;




&lt;p&gt;What helps you in your journey as a web developer? Do you have any advice to pass on to the next generation? Leave your comments below. You never know, your advice might help someone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/background"&gt;Cover photo created by rawpixel.com - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>career</category>
    </item>
    <item>
      <title>How To Get a Comment Section On Your Static Site</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Tue, 23 Jul 2019 10:41:21 +0000</pubDate>
      <link>https://dev.to/brianemilius/how-to-get-a-comment-section-on-your-static-site-5j8</link>
      <guid>https://dev.to/brianemilius/how-to-get-a-comment-section-on-your-static-site-5j8</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted on &lt;a href="http://www.brianemilius.com/static-site-comments/"&gt;www.brianemilius.com/static-site-comments/&lt;/a&gt;, 2019-07-23. Visit &lt;a href="http://www.brianemilius.com/blog"&gt;www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;In this article, I will show you how to implement a comment section on a static website with no backend. But first, let me explain what a static site is, and what a backend is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Static Site
&lt;/h2&gt;

&lt;p&gt;A static site is a website that has no dynamic content. Usually, they are written entirely with HTML, CSS, and JavaScript. Sites like these do not fetch content from an external source. Each page is unique and will work isolated from the other pages on the website.&lt;/p&gt;

&lt;p&gt;Advantages of static sites include&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ease of development&lt;/li&gt;
&lt;li&gt;easy to host&lt;/li&gt;
&lt;li&gt;works fast on slow connections&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Disadvantages might be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;low scalability&lt;/li&gt;
&lt;li&gt;hard to implement design changes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A recent trend in web development has been to use something called Static Site Generators.&lt;/p&gt;

&lt;p&gt;Static Site Generators lets the developer work with templates and web components that in the end are generated into static HTML pages.&lt;/p&gt;

&lt;p&gt;Some of the popular Static Site Generators are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hexo.io/"&gt;Hexo&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.gatsbyjs.org/"&gt;Gatsby&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Backend
&lt;/h2&gt;

&lt;p&gt;A backend in web development is for generating data and receiving and storing data. This includes data manipulation.&lt;/p&gt;

&lt;p&gt;An example of this is a website that presents weather forecasts.&lt;/p&gt;

&lt;p&gt;The backend receives a lot of data from weather satellites, weather balloons, weather stations, etc. This data needs to be translated, interpreted and stored in a database.&lt;/p&gt;

&lt;p&gt;When a visitor requests a forecast through the website, the backend will select the appropriate data, format it so it can be presented, and sends it to the visitor.&lt;/p&gt;

&lt;p&gt;Advantages of having a backend could be&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dynamic content creation results in more visits&lt;/li&gt;
&lt;li&gt;support for interaction between users&lt;/li&gt;
&lt;li&gt;separation of content and design&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some disadvantages of having a backend might include&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;depending on the scope of a project, hosting can become quite expensive&lt;/li&gt;
&lt;li&gt;backends are at a high risk of malicious attacks from hackers, so you need to know about server security&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Comments With No Backend
&lt;/h2&gt;

&lt;p&gt;Now that we know the terminology, let us have a look at one solution to the following problem:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I want to have a comment section on my static website, but I have no backend, so I don't know where to store the comments.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many ways to solve this problem. Most of them use social media platforms as the engine under the hood. Privacy is a constant concern of mine, so I want to solve my problem without using technology that tracks my users.&lt;/p&gt;

&lt;p&gt;To the rescue comes &lt;a href="https://github.com"&gt;GitHub&lt;/a&gt; and &lt;a href="https://github.com/gitalk/gitalk/"&gt;Gitalk&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;GitHub is the go-to platform for collaboration with code. Every project on GitHub exists in something called a repository. Repositories can have connected issues, and you can comment on issues. This is the technology we are going to leverage.&lt;/p&gt;

&lt;p&gt;To do this, we are going to use a nice little open-source package called Gitalk.&lt;/p&gt;

&lt;p&gt;Gitalk will let you add a fully functional comment section to any static page, as long as you have a GitHub account. This does, however, limit the use of your website comment section to people who are interested in programming and already have a GitHub account. This is why I suggest you use this method if your website topic is about programming, web development or similar.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;First, you need to go to GitHub and create an OAuth App.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to your GitHub settings page and click "Developer Settings in the left-hand menu, then click "OAuth Apps" also in the menu.&lt;/li&gt;
&lt;li&gt;Click the little "New OAuth App" button at the top.&lt;/li&gt;
&lt;li&gt;Fill out the form with your application name, the URL of your website, and an authorization callback URL. The callback URL, in this case, is the same as your website URL.&lt;/li&gt;
&lt;li&gt;Once you have registered your OAuth App, you will receive a Client ID, and a Client Secret. Save these.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Create a public repository. This is where your comments will be saved.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On GitHub, click the plus-sign the top-right corner and select "New repository"&lt;/li&gt;
&lt;li&gt;Fill in the form. Make sure to select public, not private. The only thing you need to fill in is the repository name. It could be something like "my-website-comments".&lt;/li&gt;
&lt;li&gt;Click the green "Create repository" button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is all the setup you need to do on GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;Now you need to copy and paste some code into your website pages.&lt;/p&gt;

&lt;p&gt;On the pages where you want a comment section, place this in your head section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/gitalk/dist/gitalk.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/gitalk/dist/gitalk.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will load the stylesheet and the Gitalk script into your page.&lt;/p&gt;

&lt;p&gt;Next, you need to decide where on your page you want the comments. Place this code where you need it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"gitalk-container"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, insert this script at the bottom of your page, just before the &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gitalk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Gitalk&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHub Application Client ID&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;clientSecret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHub Application Client Secret&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHub repo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;owner&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHub repo owner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHub repo owner and collaborators, only these people can initialize github issues&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;distractionFreeMode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;  &lt;span class="c1"&gt;// Facebook-like distraction free mode&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;gitalk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gitalk-container&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Insert your Client ID and Client Secret from step 1 into the script, change &lt;code&gt;owner&lt;/code&gt;, and &lt;code&gt;admin&lt;/code&gt; to your GitHub username, and &lt;code&gt;repo&lt;/code&gt; to the name of your repository that you selected in step 2.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4
&lt;/h3&gt;

&lt;p&gt;Open each page with a comment section in your browser and click the Init button. This will create an issue for the comment section on GitHub and let your visitors use the new functionality.&lt;/p&gt;

&lt;p&gt;I use this method on my website, however, I created my website with Gatsby. This means I had to use a slightly different approach. If you are interested, you can take a look at &lt;a href="https://github.com/BrianEmilius/my-personal-website"&gt;my website repository on GitHub&lt;/a&gt; to see how I did it.&lt;/p&gt;

&lt;p&gt;Let me know your thoughts on this method in the comments below!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.freepik.com/free-photos-vectors/people"&gt;Cover photo created by rawpixel.com - &lt;/a&gt;&lt;a href="http://www.freepik.com"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>static</category>
      <category>site</category>
      <category>comments</category>
      <category>github</category>
    </item>
    <item>
      <title>Optimize Your Website</title>
      <dc:creator>Brian Emilius</dc:creator>
      <pubDate>Sun, 21 Jul 2019 13:49:22 +0000</pubDate>
      <link>https://dev.to/brianemilius/optimize-your-website-fef</link>
      <guid>https://dev.to/brianemilius/optimize-your-website-fef</guid>
      <description>&lt;p&gt;&lt;em&gt;This article was originally posted on &lt;a href="https://www.brianemilius.com/optimize-your-website/" rel="noopener noreferrer"&gt;https://www.brianemilius.com/optimize-your-website/&lt;/a&gt;, 2018-04-17. Visit &lt;a href="https://www.brianemilius.com/blog" rel="noopener noreferrer"&gt;https://www.brianemilius.com/blog&lt;/a&gt; for more content.&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;If you have ever used Google Chrome's Audit tool you've probably noticed that almost no matter what you do Audit will tell you that your CSS is blocking the rendering of your site.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you take a look at the report details Google recommends loading critical parts of your styling directly in the &lt;code&gt;head&lt;/code&gt; section and deferring non-critical styling.&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbk5ehxsw3n7hsnllif43.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fbk5ehxsw3n7hsnllif43.png" alt="Google Chrome Audit performance report example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So what is critical styling? What does render-blocking even mean?&lt;/p&gt;

&lt;p&gt;When you load a website page with your browser you put it to work. From the server, your browser receives a bunch of HTML, CSS, and in most cases JavaScript. The browser needs to interpret all this code and translate it into something that looks good and is understandable to any regular page-visitor. In other words text, shapes, colours, and animations. This is what we call rendering.&lt;/p&gt;

&lt;p&gt;All stylesheets and scripts that you place in the &lt;code&gt;head&lt;/code&gt; section must first be downloaded and then treated by the browser before the browser can start interpreting what to do and show. This means that any external script or stylesheet is blocking the browser from doing its job quickly - especially on devices with a slow connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Slow Website&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;

                &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/mainstyling.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/fonts.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/fancybox.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

                &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/jquery.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/app.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/fancybox.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
                &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/googleadwords.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="c"&gt;&amp;lt;!-- contents that needs to render --&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, 7 external files need to download before the browser can even begin to start rendering. This is what we call &lt;em&gt;render-blocking&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Critical styling has to do with how we want our website to look. It has to do with the layout of your site, the most important colours, font faces and font sizes.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Solution
&lt;/h2&gt;

&lt;p&gt;I suggest a two-part solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We need to identify what our critical styling is, and&lt;/li&gt;
&lt;li&gt;defer downloading our stylesheets and scripts till after the DOM is loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Critical Styling
&lt;/h3&gt;

&lt;p&gt;What critical styling is on your website depends entirely on the individual site. You will have to determine how much or how little styling you need for the first rendering. Ask yourself this question: "How little does it take for my site to approximate the look I want?" Then choose the CSS required for this and place it in a &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; tag in the &lt;code&gt;head&lt;/code&gt; section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;A Slightly Faster Website&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="py"&gt;--colorPrimaryForeground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="py"&gt;--colorPrimaryBackground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;98%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="py"&gt;--fontStack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Verdana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="py"&gt;--fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--colorPrimaryBackground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--colorPrimaryForeground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nl"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--fontSize&lt;/span&gt;&lt;span class="p"&gt;)/&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--fontStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="py"&gt;grid-template-rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- contents that needs to render --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Defer Downloading Of Non-Critical Styling And Scripts
&lt;/h3&gt;

&lt;p&gt;When the page has rendered and the DOM is loaded we can download the rest of our stylesheets and scripts. JavaScripts are real easy to load in this manner. All you need to do is add the attribute &lt;code&gt;defer&lt;/code&gt; to the script tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/jquery.min.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/app.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/fancybox.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/googleadwords.min.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Furthermore, I consider it good praxis to place script tags at the very end of your HTML document right before the &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Stylesheets are a bit more complicated to handle. We need to write a short JavaScript function that will inject &lt;code&gt;&amp;lt;link&amp;gt;&lt;/code&gt; tags after the DOM has loaded.&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;cssLoader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&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;stylesheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;head&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stylesheet&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;This function can be used inside an event listener:&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/mainstyling.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/fonts.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/fancybox.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the entire HTML document look as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Much Faster Website&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
        &lt;span class="nd"&gt;:root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="py"&gt;--colorPrimaryForeground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;7%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="py"&gt;--colorPrimaryBackground&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hsl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0%&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;98%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="py"&gt;--fontStack&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Verdana&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="py"&gt;--fontSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1em&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--colorPrimaryBackground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--colorPrimaryForeground&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nl"&gt;font&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;normal&lt;/span&gt; &lt;span class="m"&gt;400&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--fontSize&lt;/span&gt;&lt;span class="p"&gt;)/&lt;/span&gt;&lt;span class="m"&gt;100%&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;--fontStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="py"&gt;grid-template-rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt; &lt;span class="m"&gt;50px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="c"&gt;&amp;lt;!-- contents that needs to render --&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/jquery.min.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/app.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/fancybox.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"/js/googleadwords.min.js"&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cssLoader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&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;stylesheet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;link&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;href&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stylesheet&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;text/css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementsByTagName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;head&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stylesheet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DOMContentLoaded&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/mainstyling.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/fonts.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nf"&gt;cssLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/css/fancybox.css&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only thing we now need to do to perfect this is to make sure the stylesheets are loaded even if the browser has JavaScript switched off.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;noscript&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/mainstyling.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/fonts.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"/css/fancybox.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/noscript&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try this method on your next project and see if you can notice whether it makes a difference in your Audit performance report.&lt;/p&gt;

&lt;p&gt;I would love to hear from you about your experiences with this method in the commentaries below.&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://www.freepik.com/free-photos-vectors/business" rel="noopener noreferrer"&gt;luis_molinero - &lt;/a&gt;&lt;a href="http://www.freepik.com" rel="noopener noreferrer"&gt;www.freepik.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>optmization</category>
    </item>
  </channel>
</rss>
