<?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>Forem: Valentine</title>
    <description>The latest articles on Forem by Valentine (@chiefoleka).</description>
    <link>https://forem.com/chiefoleka</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%2F37900%2Fc3714a46-4d5d-45f0-94b4-4d67e498e65d.jpeg</url>
      <title>Forem: Valentine</title>
      <link>https://forem.com/chiefoleka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/chiefoleka"/>
    <language>en</language>
    <item>
      <title>How to use inline JavaScript with HTML? You definitely like really bad code</title>
      <dc:creator>Valentine</dc:creator>
      <pubDate>Fri, 06 Apr 2018 18:08:06 +0000</pubDate>
      <link>https://forem.com/chiefoleka/how-to-use-inline-javascript-with-html-you-definitely-like-really-bad-code-1a1o</link>
      <guid>https://forem.com/chiefoleka/how-to-use-inline-javascript-with-html-you-definitely-like-really-bad-code-1a1o</guid>
      <description>&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%2Fmemegenerator.net%2Fimg%2Finstances%2F64093317%2Fhorrible-code-horrible-code-everywhere.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmemegenerator.net%2Fimg%2Finstances%2F64093317%2Fhorrible-code-horrible-code-everywhere.jpg" alt="Horrible code everywhere"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have made so many mistakes in my short coding lifetime to know never to use the DOM to call javascript methods like this:&lt;/p&gt;

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

&amp;lt;button type="submit" onclick="shoutout()"&amp;gt;YAY!&amp;lt;/button&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;Doing that is just bad, really really bad. You should never do it. Never ever ever do it. In all honesty and fairness, your code will generally work the moment you do it and everything will be fine. But, the moment you make up your mind to optimize your page... Horrible stories will be told thereafter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Few reasons to avoid inline Javascript temptation
&lt;/h2&gt;

&lt;p&gt;I didn't even need to think too much about this. Just a quick search brought me a stackoverflow thread that highlighted a lot of my reservations for inline js and even more. I'd list them here&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's not recommended to inline your javascript since you can't cache them. You definitely know how much resources caches saves your client and how fast your website loads because of it. Don't sacrifice it.&lt;/li&gt;
&lt;li&gt;Maintainability! How would you even begin to trace where you called javascript methods to start fixing it? How would you even keep track of what is using what?&lt;/li&gt;
&lt;li&gt;Separation of concerns&lt;/li&gt;
&lt;li&gt;Optimization and minification - I tried extracting the js files and minifying them, but the code never worked at all, which is why I am writing this.&lt;/li&gt;
&lt;li&gt;Inlining does not have any direct performance gains compared to external scripts. However, when you talk about caching and running scripts after you have loaded the page, it begins to make sense why not to do it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ok. I think that's all I have off the top of my head. Now, read the full story of how I got to writing this.&lt;/p&gt;

&lt;h2&gt;
  
  
  My own horrible story
&lt;/h2&gt;

&lt;p&gt;So, I met this cool project and liked the idea, so I jumped on it. It is in the fintech space and I have always loved money and fintech and money. I always wanted to know how all these things worked behind the scenes. So you can see how I jumped and accepted this with my two hands wide open when it was presented to me.&lt;/p&gt;

&lt;p&gt;As I got working on the project, it turned out that there was a developer who attempted making it work before, but couldn't, so he bailed (I still can't see why). Apparently, some of the javascript code he wrote was still all over the place in the HTML pages. As a cool guy that I am, I did not mind that at all. I happily continued working on the project and learning how to use the code that way. I just have to mention here that &lt;code&gt;class names&lt;/code&gt; and &lt;code&gt;id names&lt;/code&gt; were completely obscure and undescriptive. I have a headache thinking about it right now.&lt;/p&gt;

&lt;p&gt;I like to keep things clean, but only after getting it to work. I continued coding with my script just below the HTML as it was faster for me (which is perfectly normal by the way). When I finished getting everything to work, it was time to take all the Javascript out and put it in a separate file so I can minify it. &lt;/p&gt;

&lt;p&gt;I minified it quite alright and some parts of the app were working while others were not working. I unlinked the minified version and linked the 'unminified' version and the page worked fine. I copied and pasted all it's content again and minified one more time and it still did not work again. Something was obviously not right. &lt;/p&gt;

&lt;p&gt;I went through the page again and realised that when I minified the file, for some weird reason, every method that is called directly from the HTML as the example above somehow didn't exist anymore (well, it got minified obviously). I checked how many of such exists and I am certain I counted at least 10 before scrolling the page. There are multiple lines this way.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would do
&lt;/h2&gt;

&lt;p&gt;The best thing to do is use event listeners for everything. This way, it wouldn't really matter how I minify it, everything would just work fine. I probably have like 10 pages to rewrite and use event listeners for, but that fine. Imagine you have to do this for an application with several times that amount of pages... Sheesh!!!&lt;/p&gt;

&lt;p&gt;I'm already scared.&lt;/p&gt;

&lt;p&gt;Wish me luck... I'd definitely be needing a lot of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; &lt;em&gt;In a totally unrelated topic, please always indent your code properly and remove anything you commented out you no longer need. Take pity on the next dev who would work on it.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bestpractice</category>
      <category>html</category>
      <category>web</category>
    </item>
    <item>
      <title>How to setup Nginx and PHP7.1 with FPM on Mac OS X without crying </title>
      <dc:creator>Valentine</dc:creator>
      <pubDate>Sun, 29 Oct 2017 23:02:02 +0000</pubDate>
      <link>https://forem.com/chiefoleka/how-to-setup-nginx-and-php71-with-fpm-on-mac-os-x-without-crying-4m8</link>
      <guid>https://forem.com/chiefoleka/how-to-setup-nginx-and-php71-with-fpm-on-mac-os-x-without-crying-4m8</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fmedia.rehansaeed.com%2Frehansaeed%2F2016%2F08%2FNGINX.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/http%3A%2F%2Fmedia.rehansaeed.com%2Frehansaeed%2F2016%2F08%2FNGINX.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had problems with my nginx for months. I read a ton of articles online but couldn't find a solution. Most of what I saw was for &lt;code&gt;php7.0&lt;/code&gt;, and they used &lt;code&gt;php-fpm-sock file&lt;/code&gt; which clearly does not exist in &lt;code&gt;php7.1&lt;/code&gt;. Creating and configuring &lt;code&gt;php7.1&lt;/code&gt; to use the &lt;code&gt;sock file&lt;/code&gt; was a different kettle of fish on it's own. I failed at it several times. When I upgraded to High Sierra (which basically wiped my system clean due to multiple failures), I had to start setup all over again. I decided to take my time and actually tackle this nginx setup issue. There was still no tutorial on it that worked for me. I made up my mind to figure it out on my own. Did I? This article will explain how I did it. &lt;/p&gt;

&lt;p&gt;If you are new to http servers on your local machine, this article would make setting up nginx pretty easy for you even if you don't understand most of the stuff. If you use LAMP or WAMP or any of that, you might want to go read up on setting up nginx for your OS before coming right here. The nginx configurations would be the same. Just the folders to find the &lt;code&gt;nginx.config&lt;/code&gt; file for nginx may change from OS to OS. Yes, this should work for non Mac users as well.&lt;/p&gt;

&lt;p&gt;To make this article pretty useful to you, I'd skip a lot of stuff that are not the focus (stuff that there are probably like 1,000 articles on already). I'd speed through a lot of this. I use High Sierra on my Mac (I used Sierra before) so I cannot guarantee what would happen for other versions of macOS X. If you are following and anything is missing, do not fret. Just Google how to install it and then continue with this setup.&lt;/p&gt;

&lt;p&gt;If you already have Nginx and PHP7.1 installed, you can skip to the installation section and head straight to configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  INSTALLATION
&lt;/h2&gt;

&lt;p&gt;We are going to use &lt;code&gt;Homebrew&lt;/code&gt; for all our installations. It makes life easy and it is pretty straightforward. &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;Click here&lt;/a&gt; to read about it or just install right away.&lt;/p&gt;

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

/usr/bin/ruby &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/Homebrew/install/master/install&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;When you are done, ensure homebrew's environment is setup correctly.&lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew doctor


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

&lt;/div&gt;

&lt;p&gt;Next we're going to install a launchctl wrapper that will stop, start, and restart any service we install with brew. &lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew tap homebrew/services&lt;span class="p"&gt;;&lt;/span&gt; brew tap homebrew/dupes


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

&lt;/div&gt;

&lt;p&gt;Let's install php7.1 with fpm and display the version after installation.&lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;php71 &lt;span class="nt"&gt;--with-fpm&lt;/span&gt; &lt;span class="nt"&gt;--without-apache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; php &lt;span class="nt"&gt;-v&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; php-fpm &lt;span class="nt"&gt;-v&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Start PHP right away. &lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew services start php71 


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

&lt;/div&gt;

&lt;p&gt;We would not have to setup much for PHP7.1 with FPM because it comes pretty configured right out of the box. Let's just get nginx and get the party going.&lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew &lt;span class="nb"&gt;install &lt;/span&gt;nginx&lt;span class="p"&gt;;&lt;/span&gt; brew services start nginx


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

&lt;/div&gt;

&lt;p&gt;We are done with installations for now, time for configurations. Nginx should be running on port 8080 if you also have apache installed and running. Either way, you should see which port it is running on after installation, so you can quickly visit &lt;code&gt;http://localhost:8080&lt;/code&gt; to see the default nginx homepage (use the port displayed in your terminal after setup instead, if it is not 8080).&lt;/p&gt;

&lt;h2&gt;
  
  
  THE CONFIGURATION
&lt;/h2&gt;

&lt;p&gt;When installed with homebrew, Nginx’s configuration file would be found in &lt;code&gt;/usr/local/etc/nginx&lt;/code&gt;. Open it with any editor you like (I use &lt;code&gt;nano&lt;/code&gt; to save time).&lt;/p&gt;

&lt;p&gt;We are going to leave everything the way it is and just edit only a few sections. The &lt;code&gt;nginx.config&lt;/code&gt; file would be configured properly upon installation to serve static files. All your websites would be in &lt;code&gt;/usr/local/var/www&lt;/code&gt; folder so that nginx can see them. Nginx mirrors all the files there to it's &lt;code&gt;html&lt;/code&gt; folder that can be found in &lt;code&gt;/usr/local/Cellar/nginx/[version_number]/html&lt;/code&gt; as of this writing. If you want to set a different part for your files, edit this block of the config file and set a different &lt;code&gt;root&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;

&lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kn"&gt;root&lt;/span&gt;   &lt;span class="s"&gt;html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kn"&gt;index&lt;/span&gt;  &lt;span class="s"&gt;index.html&lt;/span&gt; &lt;span class="s"&gt;index.htm&lt;/span&gt; &lt;span class="s"&gt;index.php&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;Don't forget to add &lt;code&gt;index.php&lt;/code&gt; to the &lt;code&gt;index&lt;/code&gt; list so that nginx can automatically load that file when it serves your site. &lt;/p&gt;

&lt;p&gt;The next thing is to setup how our php files are proxied for execution. Edit your block to match the following. &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;

  &lt;span class="c1"&gt;# proxy the PHP scripts to Apache listening on 127.0.0.1:80&lt;/span&gt;
        &lt;span class="c1"&gt;#&lt;/span&gt;
        &lt;span class="c1"&gt;# location ~ \.php$ {&lt;/span&gt;
        &lt;span class="c1"&gt;#    proxy_pass   http://127.0.0.1;&lt;/span&gt;
        &lt;span class="c1"&gt;#}&lt;/span&gt;

  &lt;span class="c1"&gt;# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000&lt;/span&gt;
        &lt;span class="c1"&gt;#&lt;/span&gt;
        &lt;span class="k"&gt;location&lt;/span&gt; &lt;span class="p"&gt;~&lt;/span&gt; &lt;span class="sr"&gt;\.php$&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kn"&gt;root&lt;/span&gt;           &lt;span class="s"&gt;html&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kn"&gt;include&lt;/span&gt;        &lt;span class="s"&gt;fastcgi.conf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kn"&gt;fastcgi_pass&lt;/span&gt;   &lt;span class="nf"&gt;127.0.0.1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;9000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kn"&gt;fastcgi_index&lt;/span&gt;  &lt;span class="s"&gt;index.php&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="c1"&gt;#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;&lt;/span&gt;
            &lt;span class="kn"&gt;include&lt;/span&gt;        &lt;span class="s"&gt;fastcgi_params&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;I left the &lt;code&gt;fastcgi_param&lt;/code&gt; line commented out just incase you need to set something there later. All params are already included below it and the &lt;code&gt;fastcgi.conf&lt;/code&gt; file should have all the settings required to use &lt;code&gt;php-fpm&lt;/code&gt;. Save your file and restart nginx.&lt;/p&gt;

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

&lt;span class="nv"&gt;$ &lt;/span&gt;brew services restart nginx


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

&lt;/div&gt;

&lt;p&gt;If you are using multiple configuration files (all stored in &lt;code&gt;/sites-available&lt;/code&gt;), you would have to adjust them one by one as well. You can store this setup block above in a separate file and include it in all of them if they all use the same root folder.&lt;/p&gt;

&lt;p&gt;Time to test our beautiful setup. Create an &lt;code&gt;info.php file&lt;/code&gt; in &lt;code&gt;/usr/local/var/www&lt;/code&gt; folder and put any &lt;code&gt;php&lt;/code&gt; code of your choice that should output stuff (I echoed &lt;code&gt;phpinfo()&lt;/code&gt;). Go to &lt;code&gt;http://localhost:8080/info.php&lt;/code&gt; and it should display just fine (use the port displayed in your terminal after setup if it is not 8080).&lt;/p&gt;

&lt;p&gt;Now buddy, go enjoy your nginx and cry no more.&lt;br&gt;
&lt;/p&gt;


&lt;p&gt;NGINX is a free, open-source, high-performance HTTP server known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. If you want to see how nginx performs in handling requests and heavy usage when compared to apache, read &lt;a href="https://help.dreamhost.com/hc/en-us/articles/215945987-Web-server-performance-comparison" rel="noopener noreferrer"&gt;Web server performance comparison&lt;/a&gt;. If you would like to understand better about the individual strengths of nginx and apache, checkout &lt;a href="https://www.digitalocean.com/community/tutorials/apache-vs-nginx-practical-considerations" rel="noopener noreferrer"&gt;this article&lt;/a&gt; on Digital Ocean.&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>php71</category>
      <category>fastcgi</category>
      <category>phpfpm</category>
    </item>
  </channel>
</rss>
