<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Roberto Tonino</title>
    <description>The latest articles on DEV Community by Roberto Tonino (@robertotonino).</description>
    <link>https://dev.to/robertotonino</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%2F407169%2Ff4c7e207-50e5-41ad-b38e-719910abf233.jpg</url>
      <title>DEV Community: Roberto Tonino</title>
      <link>https://dev.to/robertotonino</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/robertotonino"/>
    <language>en</language>
    <item>
      <title>How to work in multiple branches of the same repo at the same time</title>
      <dc:creator>Roberto Tonino</dc:creator>
      <pubDate>Fri, 28 Jan 2022 12:17:19 +0000</pubDate>
      <link>https://dev.to/robertotonino/how-to-work-in-multiple-branches-of-the-same-repo-at-the-same-time-pgn</link>
      <guid>https://dev.to/robertotonino/how-to-work-in-multiple-branches-of-the-same-repo-at-the-same-time-pgn</guid>
      <description>&lt;p&gt;You can use Git &lt;strong&gt;worktrees&lt;/strong&gt; 🌲 to achieve so. Just &lt;code&gt;cd&lt;/code&gt; in a git repo and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git worktree add &amp;lt;filesystem-location&amp;gt; &amp;lt;branch&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and git will create a new folder in the path &lt;code&gt;&amp;lt;filesystem-location&amp;gt;&lt;/code&gt;  and checkout the branch &lt;code&gt;&amp;lt;branch&amp;gt;&lt;/code&gt; in that folder.&lt;/p&gt;

&lt;p&gt;For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git worktree add ../app-4.2.0 production-4.2.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can manage 2 copies of the same repo at the same time 💏🏽&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Full reference: &lt;a href="https://git-scm.com/docs/git-worktree"&gt;https://git-scm.com/docs/git-worktree&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to fix express-js CORS requests that use req.session.id</title>
      <dc:creator>Roberto Tonino</dc:creator>
      <pubDate>Wed, 05 Jan 2022 16:32:01 +0000</pubDate>
      <link>https://dev.to/robertotonino/how-to-fix-express-js-cors-requests-that-use-reqsessionid-4bkg</link>
      <guid>https://dev.to/robertotonino/how-to-fix-express-js-cors-requests-that-use-reqsessionid-4bkg</guid>
      <description>&lt;p&gt;I had a problem: I had an &lt;strong&gt;express-js&lt;/strong&gt; server serving the frontend by directly serving an HTML page.&lt;br&gt;
For the frontend development, I was using a dev script which, using Rollup, was bundling js (and css and imgs and etc...) in a single file. I decided to switch to &lt;code&gt;Vite&lt;/code&gt; for better developer experience, but I faced a problem I didn't expect: when setting up &lt;strong&gt;CORS&lt;/strong&gt; permissions (by using express' middleware), the frontend did not work properly. I will not go into details of the code.&lt;/p&gt;

&lt;p&gt;The problem was that the server used, for some requests, the &lt;code&gt;req.session.id&lt;/code&gt; string as a common ground for storing some (important!) information. In the previous setting, this string remained unique among all the requests coming from the frontend, but at this point it was not the case anymore.&lt;/p&gt;

&lt;p&gt;To resolve this problem, it was simply necessary to add the following option when calling the &lt;code&gt;fetch&lt;/code&gt; function (to&lt;br&gt;
all of them):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;endpoint&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;include&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(And obviously enabling credentials in the CORS middleware:)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;credentials&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fix, in some way that I'm still not 100% sure, helps keeping the same &lt;code&gt;req.session.id&lt;/code&gt; by passing in the HTTP&lt;br&gt;
headers the necessary cookies.&lt;/p&gt;

</description>
      <category>cors</category>
      <category>express</category>
      <category>fetch</category>
    </item>
    <item>
      <title>1 small tip to improve your code readability</title>
      <dc:creator>Roberto Tonino</dc:creator>
      <pubDate>Fri, 25 Sep 2020 12:10:24 +0000</pubDate>
      <link>https://dev.to/robertotonino/1-small-tip-to-improve-your-code-readability-3n5d</link>
      <guid>https://dev.to/robertotonino/1-small-tip-to-improve-your-code-readability-3n5d</guid>
      <description>&lt;p&gt;(&lt;a href="https://www.pexels.com/it-it/@kevin-ku-92347"&gt;Kevin Du&lt;/a&gt; photo on &lt;a href="https://www.pexels.com/"&gt;Pexels&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Code readability is important.&lt;/p&gt;

&lt;p&gt;When you find yourself in situations like this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// some code...&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="cm"&gt;/* test */&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/*

       A good amount of code

    */&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;you can refactor it in this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// some code...&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="cm"&gt;/* test */&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;

  &lt;span class="cm"&gt;/*

     A good amount of code

  */&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or, even better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;doSomething&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// some code...&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="cm"&gt;/* test */&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;needToDoALotOfThings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="cm"&gt;/* error message */&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="cm"&gt;/*

     A good amount of code

  */&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The difference is &lt;strong&gt;slight&lt;/strong&gt; but &lt;strong&gt;significant&lt;/strong&gt;. By using this approach, you'll have (at least) 2 advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;1 less indentation level, that is always good;&lt;/li&gt;
&lt;li&gt;Your condition is shrinked in 1 line of code, making the code easier to read in future reviews.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You obviously can't use this approach everywhere, it depends on the situation (as always), but it's a small correction that can save a bit of brain cells to the person that will read that snippet of code in the future.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>readability</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
