<?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: Smart Home Dan</title>
    <description>The latest articles on DEV Community by Smart Home Dan (@smarthomedan).</description>
    <link>https://dev.to/smarthomedan</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%2F641608%2F06635bb4-4cd6-4358-89f4-d4b69b2db01b.png</url>
      <title>DEV Community: Smart Home Dan</title>
      <link>https://dev.to/smarthomedan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/smarthomedan"/>
    <language>en</language>
    <item>
      <title>Create new Turborepo Monorepo with no example layout</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Tue, 22 Aug 2023 11:03:50 +0000</pubDate>
      <link>https://dev.to/smarthomedan/create-new-turborepo-monorepo-with-no-example-layout-25b2</link>
      <guid>https://dev.to/smarthomedan/create-new-turborepo-monorepo-with-no-example-layout-25b2</guid>
      <description>&lt;p&gt;Found there was no real explaination on the turboepo website on how to create a new Monorepo without having all the example code included.&lt;/p&gt;

&lt;p&gt;This is supported but just not well documented.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-turbo@canary -e https://github.com/tknickman/turborepo-empty-starter&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This command will give you an empty starter repo&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>devops</category>
    </item>
    <item>
      <title>Turborepo and ts(2307) Error - Cannot find module 'abc' or its corresponding type declarations.ts(2307)</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Tue, 15 Aug 2023 10:02:46 +0000</pubDate>
      <link>https://dev.to/smarthomedan/turborepo-and-ts2307-error-cannot-find-module-abc-or-its-corresponding-type-declarationsts2307-3e50</link>
      <guid>https://dev.to/smarthomedan/turborepo-and-ts2307-error-cannot-find-module-abc-or-its-corresponding-type-declarationsts2307-3e50</guid>
      <description>&lt;p&gt;When using Turborepo in VSCode, I have found that sometimes VScode starts throwing up 2307 errors. This is because it can't seem to see the modules installed at the root level inside a package. &lt;/p&gt;

&lt;p&gt;Turns out this is entirely just a weird Typescript issue, not Turborepo or VSCode&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/vercel/turbo/issues/4771"&gt;https://github.com/vercel/turbo/issues/4771&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution: Cntl + Shift + P &amp;gt; Restart TS Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Should clear the errors&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Jest: Could not find source file error</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Thu, 09 Sep 2021 10:41:32 +0000</pubDate>
      <link>https://dev.to/smarthomedan/jest-could-not-find-source-file-error-4ng4</link>
      <guid>https://dev.to/smarthomedan/jest-could-not-find-source-file-error-4ng4</guid>
      <description>&lt;p&gt;Occasionally I've been seeing the following error when running my Jest test suite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; @module/module@1.2.9 test C:\_Development\project
&amp;gt; jest

 FAIL  __tests__/resources/project.router.test.ts 
  ● Test suite failed to run

    Could not find source file: 'C:\_Development\project\__tests__\resources\project.actions.test.ts'.

      at getValidSourceFile (node_modules/typescript/lib/typescript.js:135637:29)
      at Object.getSemanticDiagnostics (node_modules/typescript/lib/typescript.js:135859:36)
      at doTypeChecking (node_modules/ts-jest/dist/compiler/language-service.js:11:35)
      at node_modules/ts-jest/dist/compiler/language-service.js:96:25
          at Array.forEach (&amp;lt;anonymous&amp;gt;)
      at compileFn (node_modules/ts-jest/dist/compiler/language-service.js:94:26)
      at Object.compile (node_modules/ts-jest/dist/compiler/instance.js:90:25)
      at TsJestTransformer.process (node_modules/ts-jest/dist/ts-jest-transformer.js:94:41)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:481:35)
      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:586:40)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The source files always exist in the right location - it seems like actually Jest isnt always calling the real file, but grabbing it from the cache? &lt;/p&gt;

&lt;p&gt;The solution is to pass the following arguemnts&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Obviously, alot of the time, we arn't running Jest directly, its in our package.json. In that case you can pass in an extra -- to tell the npm runner to pass the argument down into the underlying call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run test -- --clearCache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this helps! &lt;/p&gt;

</description>
      <category>node</category>
      <category>typescript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Express Validator - Do simple if checks to decide what to validate</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Thu, 02 Sep 2021 17:47:30 +0000</pubDate>
      <link>https://dev.to/smarthomedan/express-validator-do-simple-if-checks-to-decide-what-to-validate-lf9</link>
      <guid>https://dev.to/smarthomedan/express-validator-do-simple-if-checks-to-decide-what-to-validate-lf9</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR:&lt;/strong&gt; Use a custom validator and extract the core validation rules direct from validator.js&lt;/p&gt;

&lt;p&gt;I personally really dislike Express Validator, and have moved onto use alternatives like Joi and Yup. However, often you inherit code that you have to maintain. &lt;/p&gt;

&lt;p&gt;Express Validator doesnt make it very clear how to  do anything complex. All I wanted to do was check a postcode was valid if a flag was set. E,g,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
        "specialFlag": false,
        "postcode": "AB1 1AA"
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If special flag was true, we didn't need to check the postcode was a valid one - if it was false, we needed to check. Sounds simple? &lt;/p&gt;

&lt;p&gt;The solution is to write a custom validator that you embed as a middleware like this: e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    app.post(
        [
            body.custom(custValFunc),
        ],
        async (req, res....
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The custom validator just needed to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const custValFunc: CustomValidator = (input: bodyVar): boolean =&amp;gt; {
        if (!input.specialFlag) {
            #check if postcode was valid here 
            throw new Error('Postcode isnt valid, but specialFlag isnt set');
        }
        return true;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seemed easy enough - however I didn't want to write my own validator for post codes, because express validator ships with its own as a middleware - but we cant use those middlewares in the custom validator function! Heres where the pain was - it took me ages to work out how to expose the underlying function.&lt;/p&gt;

&lt;p&gt;Turns out express validator just uses validator.js - which will already be installed as a dependancy. We can use the isPostalCode function independantly. So the following code works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    import { isPostalCode } from 'validator';

    const custValFunc: CustomValidator = (input: bodyVar): boolean =&amp;gt; {
        if (!input.specialFlag) {

            if (isPostalCode(input.postcode, 'GB') === false) {  
                throw new Error('Postcode isnt valid, but specialFlag isnt set');
            }
        }
        return true;
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution works, because if specialFlag is infact set, we skip the post code validation. However if it isn't set - we validate again the proper checks, and can throw a nice error. &lt;/p&gt;

&lt;p&gt;You should be able to add your own complex checks and validation rules a bit easier than trying to use the inbuilt express validator functions that don't seem to work in the way you would expect.&lt;/p&gt;

</description>
      <category>node</category>
      <category>validation</category>
    </item>
    <item>
      <title>Extend Windows System Reserved Partition</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Sun, 01 Aug 2021 16:18:46 +0000</pubDate>
      <link>https://dev.to/smarthomedan/extend-windows-system-reserved-partition-31a4</link>
      <guid>https://dev.to/smarthomedan/extend-windows-system-reserved-partition-31a4</guid>
      <description>&lt;p&gt;&lt;strong&gt;I'm going to say sorry in advance that I didn't take printscreens while I solved this one - so I've nicked some others from the internet to explain it.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Today I tried to update my windows 10 install on my Gaming PC. However I kept seeing the following error: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm62jvvljmbg7t1fuicx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm62jvvljmbg7t1fuicx.png" alt="image" width="700" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Windows 10 couldn't be installed - We couldn't update the system reserved partition"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I opened up Disk Management (Windows Key + R  then "diskmgmt.msc")&lt;/p&gt;

&lt;p&gt;I could see the System Reserved partition was full but it only had 500MB assigned. It looked something like this... except the first drive was full:&lt;/p&gt;

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

&lt;p&gt;The rest of the disk was my 245GBish C: drive and it had a lot of free space. Of course the logical step was to extend the system partition.&lt;/p&gt;

&lt;p&gt;That didn't work because there was no unallocated space. Next step was to unallocate a few GB's from the big C drive. I did that, it all worked and I still couldn't extend. &lt;/p&gt;

&lt;p&gt;The disk looked something like this, my Reserved Drive was on the far left, in the middle was the rest of my big C: drive and the far right was my 3GB of unallocated space: &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftg3qi6y1z0r3jpns4alu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftg3qi6y1z0r3jpns4alu.png" alt="image" width="800" height="441"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Turns out you can't extend a drive into space that isnt directly right of it in this table. &lt;/p&gt;

&lt;p&gt;To solve this issue, I used a piece of software called &lt;strong&gt;IM-Magic Partition Resizer&lt;/strong&gt;: &lt;a href="https://www.resize-c.com/" rel="noopener noreferrer"&gt;https://www.resize-c.com/&lt;/a&gt; - They haven't paid me for this post by the way - I just found this software and it did the job. &lt;/p&gt;

&lt;p&gt;I used the free software to move the C: over to the far right, meaning my unallocated space would now be directly adjacent to my System Reserved space. I then activated the changes in the application. &lt;/p&gt;

&lt;p&gt;My system rebooted, and I saw the command line interface running the changes. I was only 10% this wouldnt trash my PC. Luckily it didnt, the script finished and then windows booted. &lt;/p&gt;

&lt;p&gt;Now my partitions were in the correct order, and I could extend my System Reserved partition into the unallocated space. Now my system looks like this: &lt;/p&gt;

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

&lt;p&gt;I've just retried my system updates and it worked! &lt;/p&gt;

</description>
      <category>tests</category>
      <category>test</category>
      <category>productivity</category>
      <category>testing</category>
    </item>
    <item>
      <title>NodeJS Basics: File System Usage</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Fri, 02 Jul 2021 11:10:59 +0000</pubDate>
      <link>https://dev.to/smarthomedan/nodejs-basics-file-system-usage-5789</link>
      <guid>https://dev.to/smarthomedan/nodejs-basics-file-system-usage-5789</guid>
      <description>&lt;p&gt;Accessing files from the OS in Node needs to be done in a certain way. &lt;/p&gt;

&lt;p&gt;When doing the import method, I found we were actually only loading the code at load time, and the files were being manpulated during runtime.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./payload.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method didn't work when supplying data for API's. &lt;/p&gt;

&lt;p&gt;The solution was to load in the files using the Node FileSystem module. &lt;/p&gt;

&lt;p&gt;The snippet below shows how to do it.&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filepath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;payload.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filePayloadRaw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filepath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filePayload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filePayloadRaw&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Few Notes on the above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;__dirname is an inbuilt variable to essentially give you the current directory location. This is important as when you run the node using Typescript, you will be in a src/ directory, so when you build it, the file location is wrong because you are executing from the /dist directory. __dirname gives you the runtime location. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;fs.readFileSync() needs to be passed in a character encoding. Additionally fs also provides a readFile() function which is async.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The raw result from fs is not usable until you parse it with the JSON library.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>node</category>
      <category>basics</category>
    </item>
    <item>
      <title>NodeJS Basics: Fixing Dependency Security Issues</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Thu, 17 Jun 2021 11:02:33 +0000</pubDate>
      <link>https://dev.to/smarthomedan/nodejs-basics-fixing-dependency-security-issues-3jdg</link>
      <guid>https://dev.to/smarthomedan/nodejs-basics-fixing-dependency-security-issues-3jdg</guid>
      <description>&lt;p&gt;When hitting an issue with nodejs modules having security vunerabilties, I follow the below process. Assume the problematic module is sharp version 0.22.1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating the module&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Check out the repository&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm ci&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 // That just makes sure we have every installed locally, same as the pipeline where this is failing&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm ls sharp&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 // We should now see how and where this problematic module is being used.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i sharp@0.28.2&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 // Now we install the fixed version at the top of our package.json tree. This will also update the references lower down the tree to our version. This should also update our package-lock.json file too.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm ls sharp&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 // Lets verify that we have managed to update the module references correctly.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm uninstall sharp&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 // If we dont use this module directly in our code (only in module dependancies, we can now remove it, but the version should be updated in our tree.&lt;/p&gt;

&lt;p&gt;Make sure we now commit this package.lock to the repository, and our CI pipeline should have been sorted.&lt;/p&gt;

</description>
      <category>node</category>
      <category>basics</category>
      <category>security</category>
    </item>
    <item>
      <title>Node.JS Basics: Handling Cookies</title>
      <dc:creator>Smart Home Dan</dc:creator>
      <pubDate>Thu, 17 Jun 2021 10:54:34 +0000</pubDate>
      <link>https://dev.to/smarthomedan/node-js-basics-handling-cookies-24n1</link>
      <guid>https://dev.to/smarthomedan/node-js-basics-handling-cookies-24n1</guid>
      <description>&lt;p&gt;Below is the test code I have for checking if my method for sending cookies is working.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Cookie Parsing Server&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cookieParser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cookie-parser&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/test-cookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;cookies&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// Here we should have access to the Cookies sent.&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;204&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeHead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Set-Cookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;token=encryptedstring; HttpOnly&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Access-Control-Allow-Credentials&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;listening on port 8080 for Cookies...&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;



</description>
      <category>node</category>
      <category>basics</category>
    </item>
  </channel>
</rss>
