<?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: Iván Roldán Lusich</title>
    <description>The latest articles on DEV Community by Iván Roldán Lusich (@ivaanrl).</description>
    <link>https://dev.to/ivaanrl</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%2F545507%2Fd4f52db7-2047-40af-be1f-8225d6c9bba8.jpeg</url>
      <title>DEV Community: Iván Roldán Lusich</title>
      <link>https://dev.to/ivaanrl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ivaanrl"/>
    <language>en</language>
    <item>
      <title>Solving 1 random codewars challenge in Typescript per day - Day 1</title>
      <dc:creator>Iván Roldán Lusich</dc:creator>
      <pubDate>Fri, 25 Dec 2020 23:32:49 +0000</pubDate>
      <link>https://dev.to/ivaanrl/solving-1-random-codewars-challenge-in-typescript-per-day-day-1-h48</link>
      <guid>https://dev.to/ivaanrl/solving-1-random-codewars-challenge-in-typescript-per-day-day-1-h48</guid>
      <description>&lt;h2&gt;
  
  
  Today's challenge &lt;a href="https://www.codewars.com/kata/587731fda577b3d1b0001196/train/typescript"&gt;(direct link)&lt;/a&gt;:  
&lt;/h2&gt;

&lt;p&gt;Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first letter capitalized without spaces.&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;camelCase("hello case"); // =&amp;gt; "HelloCase"
camelCase("camel case word"); // =&amp;gt; "CamelCaseWord"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  My Solution:  
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function camelCase(str: string): string {
  const splitStr = str.split(' ');

  return splitStr.map(word =&amp;gt; word.charAt(0).toUpperCase() + word.slice(1)).join('');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, this was a really simple one. Just split the original string at every space and then map over that array capitalizing every word.  &lt;/p&gt;

&lt;p&gt;Hopefully, we'll get a harder one next time. Have a nice day!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>challenge</category>
      <category>codewars</category>
    </item>
    <item>
      <title>Solving 1 random codewars challenge in Typescript per day - Day 0</title>
      <dc:creator>Iván Roldán Lusich</dc:creator>
      <pubDate>Thu, 24 Dec 2020 03:25:43 +0000</pubDate>
      <link>https://dev.to/ivaanrl/solving-1-random-codewars-challenge-in-typescript-per-day-day-0-1kbk</link>
      <guid>https://dev.to/ivaanrl/solving-1-random-codewars-challenge-in-typescript-per-day-day-0-1kbk</guid>
      <description>&lt;p&gt;Today's challenge &lt;a href="https://www.codewars.com/kata/5663f5305102699bad000056/train/typescript"&gt;(direct link)&lt;/a&gt;:&lt;br&gt;&lt;br&gt;
You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Find max(abs(length(x) − length(y)))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a1 and/or a2 are empty return -1 in each language except in Haskell (F#) where you will return Nothing (None).&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
a2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(a1, a2) --&amp;gt; 13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  My solution
&lt;/h1&gt;

&lt;p&gt;This is the original solution I came up with:&lt;br&gt;
&lt;/p&gt;

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

    public static mxdiflg = (a1:string[], a2:string[]) =&amp;gt; {
        if(a1.length === 0 || a2.length === 0) return -1;

        let a1Long = -1, a1Short = 900, a2Long = -1,a2Short = 900;

        a1.forEach((str) =&amp;gt; {
          const strLen = str.length;
          if(strLen &amp;gt; a1Long){
            a1Long = strLen;
          }

          if(strLen &amp;lt; a1Short){
            a1Short = strLen;
          }
        })

        a2.forEach((str) =&amp;gt; {
            const strLen = str.length;
            if(strLen &amp;gt; a2Long){
              a2Long = strLen;
            }

            if(strLen &amp;lt; a2Short){
              a2Short = strLen;
            }
          })

        const a1a2res = a1Long - a2Short;
        const a2a1res = a2Long - a1Short;

        if(a1a2res - a2a1res &amp;gt; a2a1res - a1a2res){
          return a1a2res
        } else {
          return a2a1res;
        }

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

&lt;/div&gt;



&lt;p&gt;As you can see, it's really verbose, but it's solving the problem. I'm going to refactor it to something better (I hope), and then I'll explain the code. &lt;/p&gt;

&lt;h2&gt;
  
  
  Refactor
&lt;/h2&gt;



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

    public static mxdiflg = (a1:string[], a2:string[]) =&amp;gt; {
        if(a1.length === 0 || a2.length === 0) return -1;

        const a1Long = Math.max(...a1.map(str =&amp;gt; str.length));
        const a1Short = Math.min(...a1.map(str =&amp;gt; str.length));
        const a2Long = Math.max(...a2.map(str =&amp;gt; str.length));
        const a2Short = Math.min(...a2.map(str =&amp;gt; str.length));

        const a1a2res = a1Long - a2Short;
        const a2a1res = a2Long - a1Short;

        if(a1a2res - a2a1res &amp;gt; a2a1res - a1a2res){
          return a1a2res
        } else {
          return a2a1res;
        }

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

&lt;/div&gt;



&lt;p&gt;That's my refactored code, it's much concise and works just as well. I could try and make it even shorter, but I think that would take away readability and that's not something I'm a fan of. &lt;/p&gt;

&lt;p&gt;All in all, the solution to this Kata wasn't that hard, I calculated the longest word in each array doing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const a1Long = Math.max(...a1.map(str =&amp;gt; str.length));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which transforms the array of strings to an array containing the length of said strings, and then with Math.max() I take the maximum value of that array.  &lt;/p&gt;

&lt;p&gt;Pretty much the exact same process for calculating the minimum length, but this time using Math.min().   &lt;/p&gt;

&lt;p&gt;After that, I check which of the two maximum distances between the sets is bigger, and return it as a result.  &lt;/p&gt;

&lt;p&gt;And that is it! First day completed!.&lt;br&gt;
I haven't done much on sites like codewars, but I really hope they get harder as time goes by, I love challenges!  &lt;/p&gt;

&lt;p&gt;See you tomorrow with another kata solved, Thanks for reading!.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>codewars</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Add FULL dev.to posts to your personal site using React.</title>
      <dc:creator>Iván Roldán Lusich</dc:creator>
      <pubDate>Wed, 23 Dec 2020 18:25:22 +0000</pubDate>
      <link>https://dev.to/ivaanrl/add-full-dev-to-posts-to-your-personal-site-using-react-48ai</link>
      <guid>https://dev.to/ivaanrl/add-full-dev-to-posts-to-your-personal-site-using-react-48ai</guid>
      <description>&lt;p&gt;I was recently creating my personal website and came to the conclusion that I wanted to add my dev.to posts there too.  &lt;/p&gt;

&lt;p&gt;After googling a bit I found out &lt;a href="https://dev.to/ugglr/coding-a-personal-blog-using-dev-to-api-react-and-react-hooks-33ei"&gt;this great post&lt;/a&gt; by &lt;a href="https://dev.to/ugglr"&gt;Carl-W&lt;/a&gt;, where he demonstrates how to fetch all the post from a single user.   &lt;/p&gt;

&lt;p&gt;But I wanted to go a bit further and display the full post on my personal website, instead of redirecting everyone to this site.&lt;br&gt;
After checking &lt;a href="https://docs.dev.to/api/"&gt;dev.to public API&lt;/a&gt; I found out that you can get the full post by doing a GET request to ('&lt;a href="https://dev.to/api/articles/articleId'"&gt;https://dev.to/api/articles/articleId'&lt;/a&gt;).  &lt;/p&gt;
&lt;h1&gt;
  
  
  The code:  
&lt;/h1&gt;
&lt;h2&gt;
  
  
  Get post function:  
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// src/helpers/getPost.ts  


export const getPost = async (articleId) =&amp;gt; {
   const URL = 'https://dev.to/api/articles/';

   try{
    const res = await fetch(URL + articleId);
    return await res.json();
   } catch (e){
    console.log('There has been an error fetching the post: ', error);
    /*
     You should handle your errors better than this, but this is just an example and I won't bother. I might do another post on how to handle errors later
    */
   }
} 

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

&lt;/div&gt;

&lt;h2&gt;
  
  
  Displaying the full post on your site:  
&lt;/h2&gt;

&lt;p&gt;Because I don't want to use DangerouslySetInnerHTML because it involves security issues if you don't sanitize the HTML properly and some other stuff, I'll be displaying my posts in markdown, and for that, I'm going to be using &lt;a href="https://github.com/remarkjs/react-markdown" rel="noopener noreferrer"&gt;react-markdown&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;You install it by calling&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add react-markdown

or

npm i react-markdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The use that I'm going to give to this library is very simple, but it's a really powerful tool, and you should check out their &lt;a href="https://github.com/remarkjs/react-markdown" rel="noopener noreferrer"&gt;full documentation&lt;/a&gt;(if you haven't yet. Already linked this above).  &lt;/p&gt;

&lt;p&gt;I also installed remark-gfm plugin to add a bit more functionalities as indicated &lt;a href="https://github.com/remarkjs/react-markdown#use-a-plugin-with-options" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;Now, to the part that everyone wants to see, the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//  /src/component/post.js
import {getPost} from '../helpers/getPost';
import ReactMarkdown from 'react-markdown';
import gfm from "remark-gfm";

//I'm getting the id in another component, but it shouldn't be too //hard, whether you are using CRA or something like nextjs/gatsby.
const Post = ({id}) =&amp;gt; {
    const [postInfo, setPostInfo] = useState();

    useEffect(() =&amp;gt;{
      const getAndSetPostInfo = async () =&amp;gt; {
          setPostinfo(await getPost(id));
      };

      getAndSetPostInfo();
    },[]);

    if(!postInfo) return null;

    const { title, body, createdAt } = postInfo;

    return {
      &amp;lt;div className={'container'}&amp;gt;
      &amp;lt;div className={'createdAt'}&amp;gt;{createdAt}&amp;lt;/div&amp;gt;
      &amp;lt;div className={'title__conainer'}&amp;gt;
        &amp;lt;h1 className={'title'}&amp;gt;{title}&amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div className={'body__container'}&amp;gt;
        &amp;lt;ReactMarkdown className={'markdown__container'} plugins={[gfm]}&amp;gt;
          {body}
        &amp;lt;/ReactMarkdown&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    }    

}


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

&lt;/div&gt;



&lt;p&gt;And that's pretty much it! You'll have to add styles to  in order to make it look nice, here's the scss code that makes mine look like this:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1g3mj723hivhyjho7kyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1g3mj723hivhyjho7kyo.png" alt="screenshot from ivanrl.dev showing how markdown was styled in that site."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Relevant scss (The one that styles the markdown):
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.markdown__container {
  line-height: 1.6em;
  font-size: 18px;

  h2 {
    font-size: 24px;
    margin-bottom: 35px;
  }

  p {
    width: 100%;
    margin-bottom: 20px;
    img {
      display: block;
      margin: auto;
      width: 100%;

      @include mquery(600) {
        width: 80%;
      }

      @include mquery(800) {
        width: auto;
      }
    }

    a {
      text-decoration: underline;
    }
  }

  pre {
    margin-bottom: 20px;
    width: 100%;
  }

  code {
    background-color: $main-color;
    padding: 5px;
    margin-top: 10px;
    margin-bottom: 10px;
    width: 100%;
    display: block;
    overflow-x: auto;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's one little caveat though, at the moment I haven't yet figured out how to make the # for heading functionality from markdown to work with this component. So I'm currently using =====  and ----- for headings, but that way only allows for h1 and h2 tags. I'll look for a fix and update this post later.  &lt;/p&gt;

&lt;p&gt;And that is it! I hope someone finds this useful! Thanks for reading! &lt;/p&gt;

</description>
      <category>portfolio</category>
      <category>markdown</category>
      <category>react</category>
    </item>
    <item>
      <title>How To Setup A NodeJS Server With Typescript</title>
      <dc:creator>Iván Roldán Lusich</dc:creator>
      <pubDate>Tue, 22 Dec 2020 20:22:25 +0000</pubDate>
      <link>https://dev.to/ivaanrl/how-to-setup-a-nodejs-express-server-with-typescript-57al</link>
      <guid>https://dev.to/ivaanrl/how-to-setup-a-nodejs-express-server-with-typescript-57al</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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxo3fxi6pe2ukpy08jqqi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxo3fxi6pe2ukpy08jqqi.png" alt="Nodejs and Typescript logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why NodeJS?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you are here, I assume that you are at least familiar with NodeJS, but if you want to jump into both Typescript and NodeJS at a time, I’ll explain briefly what NodeJS is. It’s a Javascript runtime for the backend, which means that you can write Javascript (or in our case, Typescript) in both our frontend and our backend, which makes the transition between coding in one to the other really easy. And, just to name one of its advantages, thanks to the V8 engine, which makes it really fast.&lt;/p&gt;

&lt;p&gt;But now, to the real question: &lt;strong&gt;Why Typescript?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In case you don’t know, Typescript is statically-typed Javascript. And one might think that this isn’t a good idea, because typing statically required declaring types in your variables, functions, defining interfaces, and a lot of time-consuming stuff.&lt;/p&gt;

&lt;p&gt;But I don’t see it that way, by defining your types in your code, you are setting yourself up for fewer errors and headaches since you don’t even need to run your code to realize that you’re are passing the wrong type of variable to your function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F137ntmv1cq8z7hdjy4av.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F137ntmv1cq8z7hdjy4av.png" alt="Code snippet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see how we are indicating that the function ‘subtract’ takes to numbers as arguments. And typescript indicates the developer that something is wrong with your function call by showing a red underline, if you hover over it you’ll see something like this:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6m4ohtne5q3utkw1vs5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6m4ohtne5q3utkw1vs5i.png" alt="Code warning"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Indicating that the subtract function returns a number, which can’t be assigned to subtractResult since we declared it as a string.&lt;/p&gt;

&lt;p&gt;In the line before that, you can see how we can call the function with incorrect types in its arguments either.&lt;/p&gt;

&lt;p&gt;Although in this example it doesn’t seem as important, when you have bigger programs with a lot of functions and types, getting rid of small headaches related to types of variables saves a lot of time.&lt;/p&gt;

&lt;p&gt;And that’s why we are going to be setting up a NodeJS/Express server with Typescript today. Let’s get started.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Setting up the environment&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I’m going to be using Yarn during this tutorial, but I’ll also add NPM commands, however, feel free to use any package manager you want to.&lt;/p&gt;

&lt;p&gt;Let's first call &lt;strong&gt;yarn&lt;/strong&gt; or &lt;strong&gt;npm init --y&lt;/strong&gt; to stark working on our project.&lt;/p&gt;

&lt;p&gt;Now, we’ll install all the packages we need to get started.&lt;/p&gt;

&lt;p&gt;We are going to be needing typescript and express, for obvious reasons. We are also going to need express-session to handle our sessions. Let’s go ahead and install those for now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add typescript express express-session

npm i typescript express express-session — save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One little caveat that you have to take into account when using typescript, is that external packages need to have typings, this is normally not a problem since most packages have them, but some not installed by default. If the one you are trying to use doesn’t have typings by default, you should try doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D @types/nameOfPackage 

npm i -D @types/nameOfPackage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example of this is the node, express, and express-session libraries, we need to install typings for it if we want to work with typescript. Let’s do it by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D @types/node @types/express @types/express-session 

npm i -D @types/node @types/express @types/express-session 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Creating the server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Because I’m going to be using this exact project to do more tutorials in the future, I’m going to get a proper folder structure. Create a new folder called ‘src’, and inside of it, a file called index.ts, this is going to be the entry point of our application.&lt;/p&gt;

&lt;p&gt;Don’t forget, since this is typescript, we can take advantage of ES6 imports, which I like a lot more than the ones you normally use on NodeJS.&lt;/p&gt;

&lt;p&gt;Let’s start by writing a very basic server:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffdlbca10plw224px27rk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffdlbca10plw224px27rk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 1:&lt;/strong&gt; We are importing express from our just installed express package.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 3:&lt;/strong&gt; We are initializing the express framework and storing it into the ‘app’ constant.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 5:&lt;/strong&gt; process.env.PORT is going to check your environmental variables to see if there’s a PORT defined in there, if there isn’t, it’ll default to 5000.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 7:&lt;/strong&gt; We use the listen function, already included with the Express framework, that expects the port number that the application will be using as a first parameter. It also accepts a second optional parameter, which we are using to log into the console that our app is listening in the desired port.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Running the server&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To run the server in our development environment, we are going to use the ts-node package. It comes included as a dependency in ts-node-dev, so we proceed by installing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add -D ts-node-dev  

npm i --only=dev ts-node-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we head over to our package.json file that was automatically created when you installed these packages, and under the ‘scripts’ tag, we must define one to start our server.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxyrxwdjuasfuxrxh0hx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxyrxwdjuasfuxrxh0hx6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are almost ready to start, but we first need to create a tsconfig.json file, this will tell ts-node how strict to be, as well as how to compile the code. Since we are using ES6 imports, we need to set it as a target. This is my current Typescript configuration, but feel free to use the one you find most comfortable:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffo82qyr91eeqlqtjdpyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffo82qyr91eeqlqtjdpyu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most of the options explain their functionality with their name, but you can check out available options &lt;a href="https://www.typescriptlang.org/docs/handbook/compiler-options.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We are now officially ready to start our server. Run&lt;br&gt;
&lt;/p&gt;

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

npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your server should now be up and running.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1fgbtnnz94575i27lkms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1fgbtnnz94575i27lkms.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A little bit more setup before sessions&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now that our server is running, let’s add some session handling with the help of our previously installed module express-session. We need to create a cookie-secret for our session, and it can’t be available to anyone because that would create a security risk on our application. Let’s go ahead and create some files to handle our keys.&lt;/p&gt;

&lt;p&gt;In our source directory, create a config folder, and inside of that, we are going to create three files: ‘dev.ts’, ‘prod.ts’, and ‘keys.ts’. Your directory should look like this:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb5gkaup31xilrv6b14a7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb5gkaup31xilrv6b14a7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, on ‘dev.ts’, we’ll add the following code:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcwcc60j7d72699rgxgm5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcwcc60j7d72699rgxgm5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, on ‘prod.ts’:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmfkv6fiechzm53v2agxu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmfkv6fiechzm53v2agxu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, on ‘keys.ts’:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbymumgaomi93ed8h7582.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbymumgaomi93ed8h7582.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first two files are pretty straight forward, we just define an object with our cookie-secret and export it, if it’s in production you’ll have it declared as an environmental variable so it can’t be accessed by any intruder.&lt;/p&gt;

&lt;p&gt;‘keys.ts’, however, is more involved.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 1 and 2:&lt;/strong&gt; We import our objects from the ‘dev.ts’ and ‘prod.ts’.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 4 to 6:&lt;/strong&gt; We create an interface that defines what our keys function is going to return. This ensures that the keys function will return an object that contains a key named ‘cookieSecret’, which value is a string.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 8 to 14:&lt;/strong&gt; We export a function that returns our production keys if we are in a production environment, or our development keys otherwise.  &lt;/p&gt;

&lt;p&gt;Now it’s time to add sessions to our application.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Handling sessions with express-session&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the code after adding sessions to our application:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0kgxtg4t9upyjiv4psz1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0kgxtg4t9upyjiv4psz1.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 2:&lt;/strong&gt; We import express-session to a constant called ‘session’.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 3:&lt;/strong&gt; We import our just created ‘keys’ function from its directory.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 7:&lt;/strong&gt; Destructure ‘cookieSecret’ from our ‘keys’ function.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line 9 to 19:&lt;/strong&gt; Using the built-in ‘use’ function in express, we declare a session object with some configuration. In which we include our cookieSecret constant as the session secret. You can check more about express-session configuration in the &lt;a href="https://www.npmjs.com/package/express-session" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;And that concludes this tutorial! I uploaded the final code to a &lt;a href="https://github.com/ivaanrl/nodejs-typescript-server" rel="noopener noreferrer"&gt;Github repository&lt;/a&gt; to make your life easier.&lt;br&gt;
It’s the first time I’ve ever done this, so feel free to criticize me or tell me how I can improve.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>node</category>
      <category>express</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
