<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Rémi's Blog]]></title><description><![CDATA[A new blog ...]]></description><link>https://mimiz.github.io</link><image><url>images/bordeaux_reduce.png</url><title>Rémi&apos;s Blog</title><link>https://mimiz.github.io</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 08 Sep 2017 09:24:23 GMT</lastBuildDate><atom:link href="https://mimiz.github.io/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[OpenVPN Server in a Docker Container]]></title><description><![CDATA[<div class="paragraph">
<p>Since some months now I&#8217;m a kind of digital nomad, and working remotly from Cafés, Train stations, Airports or CoWorking spaces. And often I need to "secure" a little bit my connections.</p>
</div>
<div class="paragraph">
<p>I am a Docker lover and I also have VPS at OVH, so I decided to try to create my own VPN server in a docker Container and to host it on my VPS Instance.</p>
</div>
<div class="paragraph">
<p>After a quick search on Google I found <a href="https://memo-linux.com/un-serveur-openvpn-en-moins-de-5-minutes-avec-docker/">this article</a> (in French) and it made the job !</p>
</div>
<div class="paragraph">
<p>As I wanted to details some part of the tutorial I decided to write mine based done by <a href="https://memo-linux.com/author/fred/">fred</a>.</p>
</div>
<div class="paragraph">
<p>We will use the <a href="https://hub.docker.com/r/kylemanna/openvpn/">kylemanna/openvpn</a> docker image.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>First create the <em>"datas"</em> container</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --name ovpn-data -v /etc/openvpn busybox</code></pre>
</div>
</div>
</li>
<li>
<p>Create configuration files</p>
<div class="paragraph">
<p>Please replace here <code>VPN.SERVERNAME.COM</code> by your server hostname. (should be able to connect through this name)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data --rm kylemanna/openvpn ovpn_genconfig -u udp://VPN.SERVERNAME.COM</code></pre>
</div>
</div>
</li>
<li>
<p>Generate RSA Keys</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data --rm -it kylemanna/openvpn ovpn_initpki</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here is the ouput :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/pki

Generating a 2048 bit RSA private key
........................................+++
.+++
writing new private key to '/etc/openvpn/pki/private/ca.key.XXXXDOmiGB'
Enter PEM pass phrase: <i class="conum" data-value="1"></i><b>(1)</b>
Verifying - Enter PEM pass phrase: <i class="conum" data-value="1"></i><b>(1)</b>

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.

Common Name (eg: your user, host, or server name) [Easy-RSA CA]: vpn.servername.com <i class="conum" data-value="2"></i><b>(2)</b>

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/pki/ca.crt

Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time <i class="conum" data-value="3"></i><b>(3)</b>

.......................................................................................+...........................................................................................................................................................................................+.......................................................................................+...................................................................................................................................++*++*

DH parameters of size 2048 created at /etc/openvpn/pki/dh.pem

Generating a 2048 bit RSA private key
.............................+++
..............................+++
writing new private key to '/etc/openvpn/pki/private/vpn.servername.com.key.XXXXMkJFIA'
-----
Using configuration from /usr/share/easy-rsa/openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/pki/private/ca.key: <i class="conum" data-value="1"></i><b>(1)</b>
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'vpn.servername.com'
Certificate is to be certified until Sep  6 08:42:18 2027 GMT (3650 days)

Write out database with 1 new entries
Data Base Updated
Using configuration from /usr/share/easy-rsa/openssl-1.0.cnf
Enter pass phrase for /etc/openvpn/pki/private/ca.key:

An updated CRL has been created.
CRL file: /etc/openvpn/pki/crl.pem</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Enter a secured PassPhrase</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Enter the name you entered before</td>
</tr>
<tr>
<td><i class="conum" data-value="3"></i><b>3</b></td>
<td>This is not false !!</td>
</tr>
</table>
</div>
</li>
<li>
<p>Now we can start the container</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data -d -p 1194:1194/udp --cap-add=NET_ADMIN --name openvpn-server kylemanna/openvpn</code></pre>
</div>
</div>
</li>
<li>
<p>Now we need to create the clients configuration file :</p>
<div class="paragraph">
<p>You will need to do that for all your users.</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Create a file for a user (with password <strong class="red"><em>prefered</em></strong>)</p>
<div class="paragraph">
<p>This will prompt for a passphrase, wich is the password the user will need to give when it wants to connect</p>
</div>
<div class="paragraph">
<p>This will also ask you for the Passphrase of the Server Key (defined two steps bedore)</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data --rm -it kylemanna/openvpn easyrsa build-client-full USERNAME <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Replace USERNAME by the name of the user</td>
</tr>
</table>
</div>
</li>
<li>
<p>Create a file for a user (without password )</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data --rm -it kylemanna/openvpn easyrsa build-client-full USERNAME nopass <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Replace <code>USERNAME</code> by the name of the user</td>
</tr>
</table>
</div>
</li>
<li>
<p>Retreive the client file :</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-sh" data-lang="sh">docker run --volumes-from ovpn-data --rm kylemanna/openvpn ovpn_getclient USERNAME &gt; USERNAME.ovpn <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Replace <code>USERNAME</code> by the name of the user</td>
</tr>
</table>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="paragraph">
<p>Here we are, now we need to try it ! Copy the <code>ovpn</code> file on your client, then use your default VPN Client Tool (I&#8217;m on a Mac Laptop and I currectly use Tunnelblick) to connect.</p>
</div>
<div class="paragraph">
<p><strong>It should work.</strong></p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
I had to change my DNS settings to use DNS server 8.8.8.8.
</td>
</tr>
</table>
</div>]]></description><link>https://mimiz.github.io/2017/09/08/OpenVPN-Server-in-a-Docker-Container.html</link><guid isPermaLink="true">https://mimiz.github.io/2017/09/08/OpenVPN-Server-in-a-Docker-Container.html</guid><category><![CDATA[docker]]></category><category><![CDATA[openvpn]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Fri, 08 Sep 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Jenkins Docker and Gitlab]]></title><description><![CDATA[<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>In this blog post I will explain how I configured my Jenkins (running in a docker container) to be able to clone and push on a private Gitlab git repository.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
I assume that you have docker and jenkins already installed and running
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_create_and_get_the_jenkins_key">Create and get the Jenkins key</h2>
<div class="sectionbody">
<div class="paragraph">
<p>First get the name (or the ID) of the Jenkins Container :</p>
</div>
<div class="paragraph">
<p>In a terminal type :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker ps | grep jenkins</code></pre>
</div>
</div>
<div class="paragraph">
<p>This should return something like that :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                                              NAMES
f26fa8c9bef9        jenkins/jenkins:lts   "/bin/tini -- /usr..."   41 hours ago        Up 41 hours         0.0.0.0:8080-&gt;8080/tcp, 0.0.0.0:50000-&gt;50000/tcp   jenkins</code></pre>
</div>
</div>
<div class="paragraph">
<p>The ID is <strong>f26fa8c9bef9</strong> (from first column) and the name is <strong>jenkins</strong> (from last column).</p>
</div>
<div class="paragraph">
<p>Then <em>"enter"</em> the container in order to create the SSH key :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker exec -it jenkins /bin/bash</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
You need to adapt the name (<strong>jenkins</strong>) with the container ID or your name.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>So now you the terminal prompt should have changed :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>jenkins@f26fa8c9bef9:/$</code></pre>
</div>
</div>
<div class="paragraph">
<p>Check if you already have an ssh key :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">cat ~/.ssh/id_rsa.pub</code></pre>
</div>
</div>
<div class="paragraph">
<p>If you have something else than <code>cat: /var/jenkins_home/.ssh/id_rsa.pup: No such file or directory</code> then you already have the key so you can directly go to the section &lt;Add your credentials to jenkins&gt;</p>
</div>
<div class="paragraph">
<p>To create the key execute the following command :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">ssh-keygen</code></pre>
</div>
</div>
<div class="paragraph">
<p>And do not use passphrase and accept default values :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>Generating public/private rsa key pair.
Enter file in which to save the key (/var/jenkins_home/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/jenkins_home/.ssh/id_rsa.
Your public key has been saved in /var/jenkins_home/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:MNSY7yiVerfBoYR0vCZZeS9n73cevZJl/ezUoSZcoj8 jenkins@f26fa8c9bef9
The key's randomart image is:
+---[RSA 2048]----+
|     ..=         |
|    ..B o        |
|   . =o= .       |
|    + *o+ +      |
|     * =S= o . ..|
|    o + = o + .o=|
|     o . + + o+o=|
|        . .E+o..*|
|           ....=o|
+----[SHA256]-----+</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then get your public key, by copy / paste the ouptut of this command :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">cat ~/.ssh/id_rsa.pub</code></pre>
</div>
</div>
<div class="paragraph">
<p>You should have something like that</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>jenkins@f26fa8c9:/$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDN6y54DIbEDp2gAgyu8ApzJ/tvxSu6myAkEQL3eUiLiLr5YC+uOalJ4AHkWwlmDWoT6SzlpvR+CQD2xEvsHoEumgTuUn1sNNVisIsyd19ga5yqBobM2/zhAaKtpkkuY9k1wOAZERsEIRm4Q5YTvVjNfIug/ZxVzg0xqJc0w9NGGuKNwOlBaCfjyJrhwdGU79Ijoq8sJ8SuswHc2DzvJWgdfXXd1T7w+NdOKqR+yzF5UXIm2uP6x/rVJ6OuINjFY0ODIkLvJtyvaHsIGAJZP21mJlJRPx18vAr6Phy+YmW2+UgEi2I6jxcQ+DYDT3TbTO+qpc+7KOLnRZyQKwnNu1T jenkins@f26fa8c9</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_add_your_credentials_to_jenkins">Add your credentials to jenkins</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now you created your SSH key so you can create the Credential.</p>
</div>
<div class="paragraph">
<p>Go to your jenkins, log in, and and click on left menu item <em>"Credentials"</em>, then sub item <em>"System"</em>, Select <em>"Global Credentials (unrestricted)"</em></p>
</div>
<div class="imageblock">
<div class="content">
<img src="/images/jenkins_global_credentials.png" alt="jenkins global credentials.png">
</div>
</div>
<div class="paragraph">
<p>Then click on <em>"Add Credentials"</em> in left menu.</p>
</div>
<div class="paragraph">
<p>And fill the form as this :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="/images/jenkins_add_credentials.png" alt="jenkins add credentials.png">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Kind</strong> : SSH Username with private key</p>
</li>
<li>
<p><strong>Scope</strong> : Global (Jenkins, nodes, items, all &#8230;&#8203;)</p>
</li>
<li>
<p><strong>Username</strong> : git</p>
</li>
<li>
<p><strong>Private</strong> Key : From the Jenkins master ~/.ssh</p>
</li>
<li>
<p><strong>Passphrase</strong> : empty (or your pass phrase if you have one)</p>
</li>
<li>
<p><strong>ID</strong> : empty</p>
</li>
<li>
<p><strong>Description</strong> : The Jenkins SSH KEy (or whatever you want)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Then Save.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_add_your_key_to_gitlab">Add your key to Gitlab</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now you have your ssh key, so go to your gitlab project you want to build with Jenkins, and click on "Settings", then "Repository".</p>
</div>
<div class="imageblock">
<div class="content">
<img src="/images/gitlab_settings_repository_deploy_keys.png" alt="gitlab settings repository deploy keys.png">
</div>
</div>
<div class="ulist">
<ul>
<li>
<p><strong>Title</strong> : Jenkins (Or anything else)</p>
</li>
<li>
<p><strong>Key</strong> : your public key (the output of the command <code>cat ~/.ssh/id_rsa.pub</code>)</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can check the <strong>Write access allowed</strong> if you need it.</p>
</div>
<div class="paragraph">
<p>Then save.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_create_a_jenkins_job">Create a jenkins job</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now, in Jenkins, you can create a job that use your repository, just remember to use the <em>"SSH"</em> version of your repository url :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="/images/jenkins_job_pipeline_credentials.png" alt="jenkins job pipeline credentials.png">
</div>
</div>
<div class="paragraph">
<p>And it should be able to clone !</p>
</div>
</div>
</div>]]></description><link>https://mimiz.github.io/2017/08/09/Jenkins-Docker-and-Gitlab.html</link><guid isPermaLink="true">https://mimiz.github.io/2017/08/09/Jenkins-Docker-and-Gitlab.html</guid><category><![CDATA[jenkins]]></category><category><![CDATA[gitlab]]></category><category><![CDATA[ssh]]></category><category><![CDATA[docker]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Wed, 09 Aug 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Configure docker httpd image]]></title><description><![CDATA[<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Recently I had to publish multiple application on a single machine and all applications will be publicly available on their own domains names.</p>
</div>
<div class="paragraph">
<p>As I am a docker addict, I want all my applications to be running as a docker container.</p>
</div>
<div class="paragraph">
<p>Imagine I have the following applications :</p>
</div>
<table class="tableblock frame-all grid-all spread">
<colgroup>
<col style="width: 16.6666%;">
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 16.6668%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">name</th>
<th class="tableblock halign-left valign-top">url</th>
<th class="tableblock halign-left valign-top">Container Name</th>
<th class="tableblock halign-left valign-top">Container Port (Expose)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">app1</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">www.app1.lol</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">app1-container</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">3000</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">app2</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">www.app2.lol</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">app2-container</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4000</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">app3</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">www.app3.lol</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">app3-container</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">8080</p></td>
</tr>
</tbody>
</table>
<div class="admonitionblock important">
<table>
<tr>
<td class="icon">
<i class="fa icon-important" title="Important"></i>
</td>
<td class="content">
I assume that all container are running (with or without <code>-P</code> flag)
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The thing is that I do not want to install a proxy (Apache or Nginx) on the docker host, so I wanted to create a docker container that will act as a proxy of all my applications</p>
</div>
<div class="paragraph">
<p>So I decided to use the <a href="https://hub.docker.com/_/httpd/">docker httpd:2.4</a> image.</p>
</div>
<div class="paragraph">
<p>Create your <em>project</em> directory : <code>mkdir ~/apache-proxy &amp; cd ~/apache-proxy</code></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_extract_httpd_conf">Extract httpd.conf</h2>
<div class="sectionbody">
<div class="paragraph">
<p>First you will need to "extract" the <code>httpd.conf</code> file from the image to be able to configure your apache server.</p>
</div>
<div class="paragraph">
<p>So you need to start a docker httpd container in a terminal :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker run -it --name apacheTmp httpd:2.4</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The name <em>apacheTmp</em> is just here to make extraction easy and to show you that this container will be deleted after use&#8230;&#8203;
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Then open a new terminal window (or a new sesion if you are doing it remotely)</p>
</div>
<div class="paragraph">
<p>And copy the <code>httpd.conf</code> file :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker cp apacheTmp:/usr/local/apache2/conf/httpd.conf ~/apache-proxy</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
personnaly I prefer to create a copy (to keep the original as it was) : <code>cp ~/apache-proxy/httpd.conf ~/apache-proxy/my-httpd.conf</code>.
</td>
</tr>
</table>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_configure_apache_and_vhosts">Configure apache and vhosts</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now I edited the <code>my-httpd.conf</code> file to enable modules <strong>proxy</strong> and <strong>proxy_http</strong>,</p>
</div>
<div class="paragraph">
<p>I just remove comment on the lines :</p>
</div>
<div class="paragraph">
<p><code>LoadModule proxy_module modules/mod_proxy.so</code></p>
</div>
<div class="paragraph">
<p>And</p>
</div>
<div class="paragraph">
<p><code>LoadModule proxy_http_module modules/mod_proxy_http.so</code></p>
</div>
<div class="paragraph">
<p>Then I created, at the end of the files my 3 vhosts</p>
</div>
<div class="listingblock">
<div class="title">www.app1.lol</div>
<div class="content">
<pre class="highlight"><code class="language-xml" data-lang="xml">&lt;VirtualHost *:80&gt;
  ServerName www.app1.lol
  ProxyPass / http://app1:3000/
&lt;/VirtualHost&gt;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">www.app2.lol</div>
<div class="content">
<pre class="highlight"><code class="language-xml" data-lang="xml">&lt;VirtualHost *:80&gt;
  ServerName www.app2.lol
  ProxyPass / http://app2:4000/
&lt;/VirtualHost&gt;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">www.app3.lol</div>
<div class="content">
<pre class="highlight"><code class="language-xml" data-lang="xml">&lt;VirtualHost *:80&gt;
  ServerName www.app3.lol
  ProxyPass / http://app3:8080/
&lt;/VirtualHost&gt;</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_create_docker_container">Create Docker Container</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now I can Create my <em>"proxy"</em> docker file :</p>
</div>
<div class="listingblock">
<div class="title">~/apache-proxy/Dockerfile</div>
<div class="content">
<pre class="highlight"><code class="language-Dockerfile" data-lang="Dockerfile">FROM httpd:2.4
COPY ./my-httpd.conf /usr/local/apache2/conf/httpd.conf</code></pre>
</div>
</div>
<div class="paragraph">
<p>So I can build now :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker build -t my/apache-proxy .</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then I can run it :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">docker run -d -p 80:80 --link app1-container:app1 --link app3-container:app3 --link app2-container:app2 --name my-proxy my/apache-proxy</code></pre>
</div>
</div>
<div class="paragraph">
<p>And now you should be able to access your 3 apps &#8230;&#8203;.</p>
</div>
</div>
</div>]]></description><link>https://mimiz.github.io/2017/05/18/Configure-docker-httpd-image.html</link><guid isPermaLink="true">https://mimiz.github.io/2017/05/18/Configure-docker-httpd-image.html</guid><category><![CDATA[docker]]></category><category><![CDATA[apache]]></category><category><![CDATA[httpd]]></category><category><![CDATA[proxy]]></category><category><![CDATA[httpd.conf]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Thu, 18 May 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Node.JS "Require" plays hide-and-seek]]></title><description><![CDATA[<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Some times ago (I don&#8217;t really remember when), working on a Node.Js project, I realised something about <code>require</code> &#8230;&#8203;</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_the_situation">The situation &#8230;&#8203;</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Imagine we have some datas in a Json file we want to access in different modules.</p>
</div>
<div class="listingblock">
<div class="title">datas.json</div>
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{
  "version":"0.1",
  "name":"MyApplication"
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>This <code>datas.json</code> is quite short, but for explaining the situation it is more than enough !</p>
</div>
<div class="paragraph">
<p>Now the code :</p>
</div>
<div class="listingblock">
<div class="title">index.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var module1 = require('./module1.js'),
    module2 = require('./module2.js');

module1.log();
module2.log();</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">module1.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datas.json');

module.exports = {
  log:function(){
    console.log(myDatas);
  }
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">module2.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datas.json');

module.exports = {
  log:function(){
    console.log(myDatas);
  }
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>As you can see, <code>module1</code> and <code>module2</code> are exactly same.</p>
</div>
<div class="paragraph">
<p>So if we execute the code, we will have the following output :</p>
</div>
<div class="listingblock">
<div class="title">output 1</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'MyApplication' }
{ version: 'O.1', name: 'MyApplication' }</code></pre>
</div>
</div>
<div class="paragraph">
<p>Ok, now imagine <code>module1</code> will modify some value before logging :</p>
</div>
<div class="listingblock">
<div class="title">module1.js (version 2)</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datas.json');

module.exports = {
  log:function(){
    lowerCaseName() <i class="conum" data-value="1"></i><b>(1)</b>
    console.log(myDatas);
  }
}

function lowerCaseName(){
  myDatas.name = myDatas.name.toLowerCase();
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>We just add this simple function that will lowerCase the name.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>In my idea, this should not affect the <code>module2</code> as they should be independant, so when I execute the code, here is the output :</p>
</div>
<div class="listingblock">
<div class="title">output 2</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'myapplication' }<i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Damned !</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache">Source code</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_why">Why</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Actually the reason is quite simple, when you require the file <code>datas.json</code>, the <code>require</code> function will create the object and store it in the <code>require.cache</code> object, then next time you use <code>require('./datas.json')</code> to retreive the datas, <code>require</code> will return you the value in that <code>require.cache</code> object.</p>
</div>
<div class="paragraph">
<p>So it mean that it gives you the reference to the value, so if you change one property of that value, all other modules are affected as you are actually doing the change on the reference in the cache.</p>
</div>
<div class="quoteblock">
<div class="title">This is quite well documented in the Node.js documentation :</div>
<blockquote>
<div class="paragraph">
<p>Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.</p>
</div>
</blockquote>
<div class="attribution">
&#8212; Node.Js Documentation<br>
<cite>https://nodejs.org/api/modules.html#modules_caching</cite>
</div>
</div>
<div class="listingblock">
<div class="content">
<pre>Remember that Node.Js is single thread, it mean for example that the require cache is shared accross all requests</pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_reflections_on_solutions_to_have_the_desired_behaviour">Reflections on solutions to have the desired behaviour</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now I will try to make the code work as desired.</p>
</div>
<div class="paragraph">
<p>Some of the solutions are not solving the problem, but I wanted to try to see how it works &#8230;&#8203;</p>
</div>
<div class="sect2">
<h3 id="_the_em_const_em_way_fail">The <em>Const</em> way &#8230;&#8203; FAIL</h3>
<div class="paragraph">
<p>First I wanted to use the new ES6 <code>const</code> instead of <code>var</code> in the line</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">// replace
var myDatas = require('./datas.json');
// with
const myDatas = require('./datas.json');</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">output 3</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'myapplication' }<i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Damned !</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>But actually, <code>const</code> do not prevent the variable to be modified, but prevent reafectation.</p>
</div>
<div class="paragraph">
<p><span class="red big">FAIL</span></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/const_way">Source Code, branch const_way</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_em_delete_em_way_success">The <em>delete</em> way &#8230;&#8203; SUCCESS</h3>
<div class="paragraph">
<p>With this solution, you will <code>delete</code> the reference in the <code>require.cache</code> object before require the <code>datas.json</code> file like this :</p>
</div>
<div class="listingblock">
<div class="title">module2.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">delete require.cache[require.resolve('./datas.json')];
var myDatas = require('./datas.json');
// Code continue ...</code></pre>
</div>
</div>
<div class="paragraph">
<p>As you may not know how your modules are inserted, you need to add the line on every files that need to use the <code>datas.json</code> file.</p>
</div>
<div class="paragraph">
<p>In my opinion, this is not very convenient, but it works :</p>
</div>
<div class="listingblock">
<div class="title">output 4</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'MyApplication' } <i class="conum" data-value="1"></i><b>(1)</b></code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Great !</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>I Think that this solution is the <strong><em>"Quick And Dirty"</em></strong> solution.</p>
</div>
<div class="paragraph">
<p><strong class="green big">SUCCESS</strong></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/delete_way">Source Code, branch delete_way</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_em_proxy_em_way_success">The <em>Proxy</em> Way &#8230;&#8203; SUCCESS</h3>
<div class="paragraph">
<p>With this solution I imagine to create a <code>Proxy</code> around the json datas, and override the <code>set</code> method, in order to forbid the manipulation of the value.</p>
</div>
<div class="paragraph">
<p>This approach can be usefull to throw excpetion if someone try to set the property value.</p>
</div>
<div class="paragraph">
<p>Here is the code :</p>
</div>
<div class="listingblock">
<div class="title">datasProxy.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var datas = require('./datas.json'); <i class="conum" data-value="1"></i><b>(1)</b>

module.exports = new Proxy(datas, {
  set:function(){
    return; <i class="conum" data-value="2"></i><b>(2)</b>
  }
});</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>This should be the only location in the app where you require the json file.</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Here, we just do nothing, but we could throw exception here to reject any modification of any properties.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>We also need to change the reference in <code>module1</code> and <code>module2</code> :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">// Replace
var myDatas = require('./datas.json');
// with
var myDatas = require('./datasProxy');</code></pre>
</div>
</div>
<div class="paragraph">
<p>Can you see a big problem ?</p>
</div>
<div class="paragraph">
<p>YES, the code in <code>module1</code> should be updated because now we can not set the property (even locally)</p>
</div>
<div class="paragraph">
<p>Let&#8217;s first execute the code without any modification :</p>
</div>
<div class="listingblock">
<div class="title">output 5</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'MyApplication' }
{ version: 'O.1', name: 'MyApplication' }</code></pre>
</div>
</div>
<div class="paragraph">
<p>So there is a problem, the first line should display the text MyApplciation in lowercase.
So let&#8217;s edit the code in <code>module1</code> to have the desired behaviour.</p>
</div>
<div class="paragraph">
<p>Here is a working code :</p>
</div>
<div class="listingblock">
<div class="title">module1.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = Object.assign({}, require('./datasProxy')); <i class="conum" data-value="1"></i><b>(1)</b>

module.exports = {
  log:function(){
    lowerCaseName()
    console.log(myDatas);
  }
}

function lowerCaseName(){
  myDatas.name = myDatas.name.toLowerCase();
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>We use <code>Object.assign()</code> to create a <em>"copy"</em> of the object in the <code>module1</code></td>
</tr>
</table>
</div>
<div class="paragraph">
<p>If we look at the output :</p>
</div>
<div class="listingblock">
<div class="title">output 6</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'MyApplication' }</code></pre>
</div>
</div>
<div class="paragraph">
<p>So it works !</p>
</div>
<div class="paragraph">
<p>But, as the MDN web site says:</p>
</div>
<div class="quoteblock">
<blockquote>
<div class="paragraph">
<p>"The <code>Object.assign()</code> method only copies enumerable and own properties from a source object to a target object."</p>
</div>
</blockquote>
<div class="attribution">
&#8212; Mozilla Developper Network<br>
<cite>http://devdocs.io/javascript/global_objects/object/assign</cite>
</div>
</div>
<div class="paragraph">
<p>Maybe you should use a "clone" function that allow to clone objects deeply (check this with lodash <a href="https://lodash.com/docs/4.17.4#clone"><code>_.clone()</code></a> for example)</p>
</div>
<div class="paragraph">
<p>I think this solution is not too bad, but the thing is that you delegate to the module the need to create a copy, maybe this suits your needs, or maybe we can do it in the proxy itself.</p>
</div>
<div class="paragraph">
<p>I think both solution can be justified, you just need to make a choice.</p>
</div>
<div class="paragraph">
<p><strong class="green big">SUCCESS</strong></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/proxy_way">Source Code, branch proxy_way</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_em_clone_em_way_success">The <em>Clone</em> Way  &#8230;&#8203; SUCCESS</h3>
<div class="paragraph">
<p>This solution is the solution I considered previously.</p>
</div>
<div class="paragraph">
<p>So with this solution, the <em>"Proxy"</em> (or you can call it, the <em>"wrapper"</em>) will create the copy and returns it to the modules :</p>
</div>
<div class="paragraph">
<p>So edit the Proxy code, and the modules :</p>
</div>
<div class="listingblock">
<div class="title">datasProxy.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var datas = require('./datas.json');

module.exports = function(){ <i class="conum" data-value="1"></i><b>(1)</b>
  return clone(datas);
}

function clone(datas){
  return JSON.parse(JSON.stringify(datas)); <i class="conum" data-value="2"></i><b>(2)</b>
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>We export a function that need to be called in module to return a copy of the datas</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Here we use a hack to clone <em>"deeply"</em> an object.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Then edit the modules to call the exported function</p>
</div>
<div class="listingblock">
<div class="title">module1.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datasProxy')(); <i class="conum" data-value="1"></i><b>(1)</b>

module.exports = {
  log:function(){
    lowerCaseName()
    console.log(myDatas);
  }
}

function lowerCaseName(){
  myDatas.name = myDatas.name.toLowerCase();
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Call the function to get the copy.</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="title">module2.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datasProxy')(); <i class="conum" data-value="1"></i><b>(1)</b>

module.exports = {
  log:function(){
    console.log(myDatas);
  }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Call the function to get the copy.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Then execute the code :</p>
</div>
<div class="listingblock">
<div class="title">output 7</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'MyApplication' }</code></pre>
</div>
</div>
<div class="paragraph">
<p>Great it works !</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
This is my favorite solution.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><strong class="green big">SUCCESS</strong></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/clone_way">Source Code, branch clone_way</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_em_decache_em_way_success">The <em>decache</em> way &#8230;&#8203; SUCCESS</h3>
<div class="paragraph">
<p>This idea was given to me by <a href="https://jermor.in/">Jérémy  Morin</a>.</p>
</div>
<div class="paragraph">
<p><a href="https://www.npmjs.com/package/decache">Decache</a> is a Node Module, that remove module from the <code>require</code> cache.</p>
</div>
<div class="paragraph">
<p>This solution has exactly the same effect than the <em>delete</em> solution I present before. But it make it easier to do.</p>
</div>
<div class="paragraph">
<p>First you need to install the <code>decache</code> node module :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">npm init <i class="conum" data-value="1"></i><b>(1)</b>
npm install decache --save</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>This is not necessary if you already initialise a node project.</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Then edit the code :</p>
</div>
<div class="listingblock">
<div class="title">index.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var module1 = require('./module1.js'),
    module2 = require('./module2.js');

module1.log();
module2.log();</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">module1.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var myDatas = require('./datas.json');

module.exports = {
  log:function(){
    lowerCaseName()
    console.log(myDatas);
  }
}

function lowerCaseName(){
  myDatas.name = myDatas.name.toLowerCase();
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">module2.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">var decache = require('decache'); <i class="conum" data-value="1"></i><b>(1)</b>
decache('./datas.json'); <i class="conum" data-value="2"></i><b>(2)</b>
var myDatas = require('./datas.json');

module.exports = {
    log: function() {
        console.log(myDatas);
    }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>First we need to require the <em>decache</em> module</td>
</tr>
<tr>
<td><i class="conum" data-value="2"></i><b>2</b></td>
<td>Then we use <em>decache</em> to remove the datas.json file from the cache</td>
</tr>
</table>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Maybe we should use <em>decache</em> from both <code>module1.js</code> and <code>module2.js</code> because if we invert the the two <em>require</em> lines in the <code>index.js</code> file the cache is not removed as we expect.
</td>
</tr>
</table>
</div>
<div class="listingblock">
<div class="title">output 7</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'MyApplication' }</code></pre>
</div>
</div>
<div class="paragraph">
<p>It works !</p>
</div>
<div class="paragraph">
<p><strong class="green big">SUCCESS</strong></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/decache_way">Source code, branch decache_way</a></p>
</div>
</div>
<div class="sect2">
<h3 id="_the_em_import_em_way_aka_the_em_es6_em_way_fail">The <em>import</em> Way (aka. the <em>ES6</em> way) &#8230;&#8203; FAIL</h3>
<div class="paragraph">
<p>This idea was also given to me by <a href="https://jermor.in/">Jérémy  Morin</a>.</p>
</div>
<div class="paragraph">
<p>As Node.JS (version 7.4.0 on my laptop) is not compatible with the ES6 (import) syntax, we will need to <em>"babelize"</em> (transpile with babel) the code, and for that we need to refactor our code, and initilaze a npm project.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>First create a <code>src</code> doirectory and copy all files inside that directory</p>
</li>
<li>
<p>In a terminal window run the <code>npm init</code> command and answer all question</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">npm init</code></pre>
</div>
</div>
</li>
<li>
<p>Then install the <code>babel-cli</code> dependency</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">npm install babel-cli --save-dev</code></pre>
</div>
</div>
</li>
<li>
<p>Create a <em>"build"</em> script</p>
<div class="listingblock">
<div class="title">package.json</div>
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{
  "name": "require_strange",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel src -d lib" <i class="conum" data-value="1"></i><b>(1)</b>
  },
  "author": "",
  "license": "ISC",
  "dependencies": {},
  "devDependencies": {
    "babel-cli": "^6.24.1"
  }
}</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Now you can use <code>npm run build</code> to build from ES6 code</td>
</tr>
</table>
</div>
</li>
<li>
<p>Configure babel :</p>
<div class="olist loweralpha">
<ol class="loweralpha" type="a">
<li>
<p>Install the <em>presets</em></p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">npm install babel-preset-env --save-dev</code></pre>
</div>
</div>
</li>
<li>
<p>Create the <code>.babelrc</code></p>
<div class="listingblock">
<div class="title">/.babelrc</div>
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{
  "presets":["env"]
}</code></pre>
</div>
</div>
</li>
</ol>
</div>
</li>
<li>
<p>Edit the Code to convert it to ES6</p>
<div class="listingblock">
<div class="title">/src/datas.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">export default {
  "version": "0.1",
  "name": "MyApplication"
};</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">/src/index.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">import module1 from './module1';
import module2 from './module2';

module1.log();
module2.log();</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">/src/module1.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">import myDatas from './datas';

export default {
    log: function() {
        lowerCaseName();
        console.log(myDatas);
    }
};

function lowerCaseName() {
    myDatas.name = myDatas.name.toLowerCase();
}</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">/src/module2.js</div>
<div class="content">
<pre class="highlight"><code class="language-js" data-lang="js">import myDatas from './datas';


export default {
    log: function() {
        console.log(myDatas);
    }
}</code></pre>
</div>
</div>
</li>
<li>
<p>Build</p>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">npm run build</code></pre>
</div>
</div>
</li>
<li>
<p>Run</p>
<div class="listingblock">
<div class="title">output 9</div>
<div class="content">
<pre class="highlight"><code class="language-zsh" data-lang="zsh">⇒  node index.js
{ version: 'O.1', name: 'myapplication' }
{ version: 'O.1', name: 'myapplication' }</code></pre>
</div>
</div>
</li>
</ol>
</div>
<div class="paragraph">
<p>As you can see the execution output exactly the same, so this is not a good solution.</p>
</div>
<div class="paragraph">
<p><strong class="red big">FAIL</strong></p>
</div>
<div class="paragraph">
<p><a href="https://github.com/mimiz/node_require_cache/tree/import_way">Source code, branch import_way</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_conclusion">Conclusion</h2>
<div class="sectionbody">
<div class="paragraph">
<p>As a conclusion, I would say that it is important to understand how the <code>require</code> cache works, that&#8217;s why I wrote this article.</p>
</div>
<div class="paragraph">
<p>In order to solve the problem presented in the introduction, I would use the <em>"clone"</em> solution, by creating a <em>"wrapper"</em> object, maybe using the <code>Proxy</code> from ES6.</p>
</div>
<div class="paragraph">
<p>Please feel free to make any comments &#8230;&#8203;</p>
</div>
</div>
</div>]]></description><link>https://mimiz.github.io/2017/04/18/NodeJS-Require-plays-hide-and-seek.html</link><guid isPermaLink="true">https://mimiz.github.io/2017/04/18/NodeJS-Require-plays-hide-and-seek.html</guid><category><![CDATA[node.js]]></category><category><![CDATA[require]]></category><category><![CDATA[require.cache]]></category><category><![CDATA[const]]></category><category><![CDATA[decache]]></category><category><![CDATA[cache]]></category><category><![CDATA[clone]]></category><category><![CDATA[javascript]]></category><category><![CDATA[js]]></category><category><![CDATA[json]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Tue, 18 Apr 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Debian Install Node.JS globally with NVM]]></title><description><![CDATA[<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Recently I had to install Node.JS on a server and allow all users to be able to use it.
As I like the nvm concept to be able to easily install Node.Js, I would like to be able to install Node via NVM, and make Node.Js available for all users. I do not want users to be able to install their own version of Node.JS, and I want to control the Node.js global packages on that computer.</p>
</div>
<div class="paragraph">
<p>So we will do the following</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_install_some_usefull_packages">Install some usefull packages</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="title">Run as root</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">apt-get install git vim curl build-essential sudo</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_install_nvm_as_root_superuser">Install nvm as "root" / superuser</h2>
<div class="sectionbody">
<div class="paragraph">
<p>First you need to clone the nvm repository :</p>
</div>
<div class="listingblock">
<div class="title">clone nvm</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">git clone https://github.com/creationix/nvm.git /opt/nvm</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then create the directory <code>/usr/local/nvm</code></p>
</div>
<div class="listingblock">
<div class="title">create diretory</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">mkdir /usr/local/nvm</code></pre>
</div>
</div>
<div class="paragraph">
<p>The edit the <code>~/.bashrc</code> and add the following line to the file :</p>
</div>
<div class="listingblock">
<div class="title">~/.bashrc</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">export NVM_DIR=/usr/local/nvm
source /opt/nvm/nvm.sh</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then logout and login again, or type <code>source ~/.bashrc</code> to enable nvm</p>
</div>
<div class="paragraph">
<p>Try nvm :</p>
</div>
<div class="listingblock">
<div class="title">try nvm</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">nvm install 6.10.1</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now <em>nvm</em> is installed for root, but not all users.</p>
</div>
<div class="paragraph">
<p>To activate the node installation for users, we will add the script <code>/etc/profile.d/nvm.sh</code> :</p>
</div>
<div class="listingblock">
<div class="title">/etc/profile.d/nvm.sh</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">#!/bin/bash
VERSION=`cat /usr/local/nvm/alias/default`
export PATH="/usr/local/nvm/versions/node/v$VERSION/bin:$PATH"</code></pre>
</div>
</div>
<div class="paragraph">
<p>Then make that file runnable : <code>chmod +x /etc/profile.d/nvm.sh</code></p>
</div>
<div class="sect2">
<h3 id="_as_user">As user</h3>
<div class="paragraph">
<p>Now connect as a user, and try to use node :</p>
</div>
<div class="listingblock">
<div class="title">try node</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">node -v</code></pre>
</div>
</div>
<div class="paragraph">
<p>You should see the node version installed before (6.10.1)</p>
</div>
<div class="paragraph">
<p>Then try to install a new version of  node :</p>
</div>
<div class="listingblock">
<div class="title">try node</div>
<div class="content">
<pre class="highlight"><code class="language-bash" data-lang="bash">nvm install 7.8.0</code></pre>
</div>
</div>
<div class="paragraph">
<p>Result should be <code>nvm : commande introuvable</code></p>
</div>
</div>
<div class="sect2">
<h3 id="_install_some_global_packages">Install some global packages</h3>
<div class="paragraph">
<p>With node you may want to install some package globbaly (<code>forever</code> for example), and I think we should not allow users to install what they want so we may install global packages as root also.</p>
</div>
<div class="paragraph">
<p>As root run  the command <code>npm install -g nodemon</code> for example, then login as a user, and run <code>nodemon index.js</code> and it works.</p>
</div>
<div class="paragraph">
<p>So all global package should be installed as root (or other superuser).</p>
</div>
</div>
</div>
</div>]]></description><link>https://mimiz.github.io/2017/04/02/Debian-Install-NodeJS-globally-with-NVM.html</link><guid isPermaLink="true">https://mimiz.github.io/2017/04/02/Debian-Install-NodeJS-globally-with-NVM.html</guid><category><![CDATA[node.js]]></category><category><![CDATA[nvm]]></category><category><![CDATA[installation]]></category><category><![CDATA[debian]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Sun, 02 Apr 2017 00:00:00 GMT</pubDate></item><item><title><![CDATA[Use Authorization Header with Swagger]]></title><description><![CDATA[<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>Recently, on a project I had to document an API using <a href="http://swagger.io/">Swagger</a>, as an authentication mechanism, the API, is using JWT.</p>
</div>
<div class="paragraph">
<p>The Token needs to be set in the <code>Authorization</code> Header of the HTTP request as this :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>Authorization Bearer: JWT-token</code></pre>
</div>
</div>
<div class="paragraph">
<p>As we wanted to use the <a href="http://swagger.io/swagger-ui/">Swagger UI</a> to allow clients to test requests and responses of the API.</p>
</div>
<div class="paragraph">
<p>To define the API, we use  the <a href="http://swagger.io/swagger-editor/">Swagger Editor</a> Online.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_start_the_documentation_and_the_login_route">Start the Documentation and the /login route</h2>
<div class="sectionbody">
<div class="paragraph">
<p>First we need to create the main API description :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-yaml" data-lang="yaml">swagger: '2.0'
info:
 version: 0.0.1
 title: MY-SUPERB-API
host: '127.0.0.1:4000'
schemes:
 - http
 - https
produces:
 - application/json
paths:
 /login:
  post:
   tags:
    - auth
   description: |
    Allow users to log in, and to receive a Token
   parameters:
    -
     in: body
     name: body
     description: The email/password
     required: true
     schema:
      $ref: '#/definitions/Login'
   responses:
    '200':
     description: Login Success
     schema:
      $ref: '#/definitions/Token'
    '400':
     description: Whether the user is not found or error while login
     schema:
      $ref: '#/definitions/Error'
    '403':
     description: &gt;-
      If user is not found (bad credentials) OR if user can not login (a
      concierge of an unsctive client)
     schema:
      $ref: '#/definitions/Error'
definitions:
 Login:
  type: object
  properties:
   email:
    type: string
   password:
    type: string
 Token:
  type: object
  properties:
   token:
    type: string
 Error:
  type: object
  properties:
   message:
    type: string
   error:
    type: string</code></pre>
</div>
</div>
<div class="paragraph">
<p>This will define the <code>/login</code> route that receive a <code>POST</code> HTTP request with email and password, if they matches your credential provider the route will return a <code>Token</code> response :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{
  "token":"JWT TOKEN"
}</code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_describe_the_security_definition">Describe the Security Definition</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Now we should define create a <strong>securityDefinitions</strong>, this will describe how authorization is made, and allow the <a href="http://swagger.io/swagger-ui/">Swagger UI</a> to ask for a token when needed.</p>
</div>
<div class="paragraph">
<p>So first edit the API definition :</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-yaml" data-lang="yaml">swagger: '2.0'
info:
 version: 0.0.1
 title: MY-SUPERB-API
host: '127.0.0.1:4000'
schemes:
 - http
 - https
produces:
 - application/json
securityDefinitions: <i class="conum" data-value="1"></i><b>(1)</b>
 Bearer:
  description: |
   For accessing the API a valid JWT token must be passed in all the queries in
   the 'Authorization' header.


   A valid JWT token is generated by the API and retourned as answer of a call
   to the route /login giving a valid user &amp; password.


   The following syntax must be used in the 'Authorization' header :

       Bearer: xxxxxx.yyyyyyy.zzzzzz
  type: apiKey
  name: Authorization
  in: header
paths:
 /login:
  post:
   tags:
    - auth
   description: |
    Allow users to log in, and to receive a Token
   parameters:
    -
     in: body
     name: body
     description: The email/password
     required: true
     schema:
      $ref: '#/definitions/Login'
   responses:
    '200':
     description: Login Success
     schema:
      $ref: '#/definitions/Token'
    '400':
     description: Whether the user is not found or error while login
     schema:
      $ref: '#/definitions/Error'
    '403':
     description: &gt;-
      If user is not found (bad credentials) OR if user can not login (a
      concierge of an unsctive client)
     schema:
      $ref: '#/definitions/Error'
definitions:
 Login:
  type: object
  properties:
   email:
    type: string
   password:
    type: string
 Token:
  type: object
  properties:
   token:
    type: string
 Error:
  type: object
  properties:
   message:
    type: string
   error:
    type: string</code></pre>
</div>
</div>
<div class="colist arabic">
<table>
<tr>
<td><i class="conum" data-value="1"></i><b>1</b></td>
<td>Ajout de la clé <strong>securityDefinitions</strong></td>
</tr>
</table>
</div>
<div class="paragraph">
<p>The description is, I think, self described, so you can understand it</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_create_a_protected_route">Create a protected route</h2>
<div class="sectionbody">
<div class="paragraph">
<p>And now you can define a route <code>/users</code> for example that needs the user to send a valid Authorization header  &#8230;&#8203;</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-yaml" data-lang="yaml">swagger: '2.0'
info:
 version: 0.0.1
 title: MY-SUPERB-API
host: '127.0.0.1:4000'
schemes:
 - http
 - https
produces:
 - application/json
securityDefinitions:
 Bearer:
  description: |
   For accessing the API a valid JWT token must be passed in all the queries in
   the 'Authorization' header.


   A valid JWT token is generated by the API and retourned as answer of a call
   to the route /login giving a valid user &amp; password.


   The following syntax must be used in the 'Authorization' header :

       Bearer: xxxxxx.yyyyyyy.zzzzzz
  type: apiKey
  name: Authorization
  in: header
paths:
 /login:
  post:
   tags:
    - auth
   description: |
    Allow users to log in, and to receive a Token
   parameters:
    -
     in: body
     name: body
     description: The email/password
     required: true
     schema:
      $ref: '#/definitions/Login'
   responses:
    '200':
     description: Login Success
     schema:
      $ref: '#/definitions/Token'
    '400':
     description: Whether the user is not found or error while login
     schema:
      $ref: '#/definitions/Error'
    '403':
     description: &gt;-
      If user is not found (bad credentials) OR if user can not login (a
      concierge of an unsctive client)
     schema:
      $ref: '#/definitions/Error'
 /users:
  get:
   tags:
    - users
   security:
    - Bearer: []
   description: Get a list of all existing users
   responses:
    '200':
     description: An array of users
     schema:
      type: array
      items:
       $ref: '#/definitions/User'
definitions:
 Login:
  type: object
  properties:
   email:
    type: string
   password:
    type: string
 Token:
  type: object
  properties:
   token:
    type: string
 Error:
  type: object
  properties:
   message:
    type: string
   error:
    type: string
 User:
    type: object
    properties:
     email:
      type: string
     password:
      type: string
     firstName:
      type: string
     lastName:
      type: string
     lastLogin:
      type: string
      format: date</code></pre>
</div>
</div>
<div class="paragraph">
<p>Now you just described that the <code>/users</code> route need the Bearer authentication &#8230;&#8203;</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_use_swagger_ui">Use Swagger UI</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In order to use the Swagger UI, you should be aware that the <strong>host</strong> value is defined on <em>'127.0.0.1:4000'</em> and this may be changed in your case !</p>
</div>
<div class="sect2">
<h3 id="_first_login_and_get_the_token">First Login and get the token</h3>
<div class="paragraph">
<p>The now you can use the <a href="http://swagger.io/swagger-ui/">Swagger UI</a> (with the editor) to login :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-1.png" alt="swagger auth 1.png">
</div>
</div>
<div class="paragraph">
<p>And the response should be :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-2.png" alt="swagger auth 2.png">
</div>
</div>
</div>
<div class="sect2">
<h3 id="_authenticate_the_editor">Authenticate the editor :</h3>
<div class="paragraph">
<p>Now at the top of the generated documentation you can click the <strong>Authenticate</strong> Button :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-3.png" alt="swagger auth 3.png">
</div>
</div>
<div class="paragraph">
<p>Then fill with the Token you received at the previous step :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-4.png" alt="swagger auth 4.png">
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
We are spcifying the <strong>Bearer:</strong> keyword.
</td>
</tr>
</table>
</div>
</div>
<div class="sect2">
<h3 id="_try_the_protected_route">Try the protected route</h3>
<div class="paragraph">
<p>Now in the section of the protected route : <code>/users</code> you can check the <em>Bearer</em> checkbox and try the operation :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-5.png" alt="swagger auth 5.png">
</div>
</div>
<div class="paragraph">
<p>You should have a valid answer :</p>
</div>
<div class="imageblock">
<div class="content">
<img src="https://mimiz.github.io/images/swagger-auth-6.png" alt="swagger auth 6.png">
</div>
</div>
<div class="paragraph">
<p>Now you can add your other routes to finish defined your API.</p>
</div>
<div class="paragraph">
<p>Have fun !</p>
</div>
</div>
</div>
</div>]]></description><link>https://mimiz.github.io/2016/12/05/Use-Authorization-Header-with-Swagger.html</link><guid isPermaLink="true">https://mimiz.github.io/2016/12/05/Use-Authorization-Header-with-Swagger.html</guid><category><![CDATA[Swagger]]></category><category><![CDATA[Authorization]]></category><category><![CDATA[Bearer]]></category><category><![CDATA[API]]></category><category><![CDATA[Express]]></category><category><![CDATA[JWT]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Mon, 05 Dec 2016 00:00:00 GMT</pubDate></item><item><title><![CDATA[How to use events in Node.js]]></title><description><![CDATA[<div class="paragraph">
<p>In this blog post I will explain how to create and use Events in Node.Js
The main purpose is to demonstrate the following :</p>
</div>
<div class="paragraph">
<p>I have a module (a) that will do some operations, and I want that module to emit an event when some operations are done.
Then I would like other modules (such as b module) to be able to subscribe to those operations.</p>
</div>
<div class="paragraph">
<p>In order to solve this I will create an EventManager object based on node events.</p>
</div>
<div class="paragraph">
<p>Create the files :</p>
</div>
<div class="listingblock">
<div class="title">a.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var operations = {
  add:function(){
    // operation for add
  },
  remove:function(){
    // operation for remove
  },
  update:function(){
    // operation for update
  }
}
module.exports = operations;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">b.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var b = {
  someCodeWhenAddInA:function(){
    // this function should be executed after a.add is executed
  }
}
module.exports = b;</code></pre>
</div>
</div>
<div class="paragraph">
<p>So the purpose is more than simple, when <code>a.add</code> is called, we want the function <code>b.someCodeWhenAddInA</code> to be called, and we also want to easylly add the ability to add modules that will execute code on <code>a.add</code> called.</p>
</div>
<div class="paragraph">
<p>Let&#8217;s create the <code>EventManager</code>.</p>
</div>
<div class="listingblock">
<div class="title">EventManager.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var util         = require("util");
var EventEmitter = require("events").EventEmitter;
function EventManager(){
    EventEmitter.call(this);
}

util.inherits(EventManager, EventEmitter);

module.exports = new EventManager();</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
refer to the documentation to know more about <a href="https://nodejs.org/api/events.html">node js events</a>
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Edit the code in <code>a</code> and <code>b</code> modules :</p>
</div>
<div class="listingblock">
<div class="title">a.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var EventManager = require('./EventManager');
var operations = {
  add:function(){
    // operation for add
    EventManager.emit('addedToA');
  },
  remove:function(){
    // operation for remove
  },
  update:function(){
    // operation for update
  }
}
module.exports = operations;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">b.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var EventManager = require('./EventManager');
var b = {
  someCodeWhenAddInA:function(){
    // this function should be executed after a.add is executed
    console.log('someCodeWhenAddInA has been executed in b');
  }
}
EventManager.on('addedToA', b.someCodeWhenAddInA);
module.exports = b;</code></pre>
</div>
</div>
<div class="paragraph">
<p>And now let&#8217;s simply create a runner file to try our code</p>
</div>
<div class="listingblock">
<div class="title">main.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var a = require('./a');
var b = require('./b');

a.add();</code></pre>
</div>
</div>
<div class="paragraph">
<p>This should print <strong><em>someCodeWhenAddInA has been executed in b</em></strong> in the console.</p>
</div>
<div class="paragraph">
<p>And that&#8217;s all !</p>
</div>
<div class="paragraph">
<p>Now if you want to create a <code>c</code> module which will listen to the same event, it&#8217;s easy, and no need to edit the <code>a</code> module anymore :</p>
</div>
<div class="listingblock">
<div class="title">c.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var EventManager = require('./EventManager');
var c = {
  anotherFonction:function(){
    // this function should be executed after a.add is executed
    console.log('anotherFonction has been executed in c');
  }
}
EventManager.on('addedToA', c.anotherFonction);
module.exports = b;</code></pre>
</div>
</div>
<div class="listingblock">
<div class="title">main.js</div>
<div class="content">
<pre class="highlight"><code class="language-javascript" data-lang="javascript">var a = require('./a');
var b = require('./b'); <i class="conum" data-value="1"></i><b>(1)</b>
var c = require('./c'); <i class="conum" data-value="2"></i><b>(2)</b>

a.add();</code></pre>
</div>
</div>
<div class="paragraph">
<p>This will print :</p>
</div>
<div class="listingblock">
<div class="content">
<pre>someCodeWhenAddInA has been executed in b
anotherFonction has been executed in c</pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If you inverse the <strong>1</strong> and <strong>2</strong> the log will also be impacted.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>Here we are, comments are appreciated</p>
</div>]]></description><link>https://mimiz.github.io/2016/11/28/How-to-use-events-in-Nodejs.html</link><guid isPermaLink="true">https://mimiz.github.io/2016/11/28/How-to-use-events-in-Nodejs.html</guid><category><![CDATA[Node.js]]></category><category><![CDATA[Events]]></category><dc:creator><![CDATA[mimiz33]]></dc:creator><pubDate>Mon, 28 Nov 2016 00:00:00 GMT</pubDate></item></channel></rss>