<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: harshchandwani</title>
    <description>The latest articles on Forem by harshchandwani (@harshchandwani).</description>
    <link>https://forem.com/harshchandwani</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%2F501012%2F348e7b61-40a3-4e14-80bc-f64b4f3458a1.jpg</url>
      <title>Forem: harshchandwani</title>
      <link>https://forem.com/harshchandwani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/harshchandwani"/>
    <language>en</language>
    <item>
      <title>Middleware to Asp.net Core MVC Application + Custom Middleware</title>
      <dc:creator>harshchandwani</dc:creator>
      <pubDate>Sun, 02 Jun 2024 05:02:01 +0000</pubDate>
      <link>https://forem.com/harshchandwani/middleware-to-aspnet-core-mvc-application-custom-middleware-4k4h</link>
      <guid>https://forem.com/harshchandwani/middleware-to-aspnet-core-mvc-application-custom-middleware-4k4h</guid>
      <description>&lt;p&gt;Hello everyone,&lt;/p&gt;

&lt;p&gt;Today, I’m excited to guide you through the process of adding custom middleware to your application. Middleware is a vital component of any application, and understanding how to effectively use it can greatly enhance your development process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Middleware Matters
&lt;/h2&gt;

&lt;p&gt;Middleware is crucial in handling all requests and responses in your application. By leveraging middleware, you can streamline and manage various tasks such as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt;: Verifying user identity.&lt;br&gt;
&lt;strong&gt;Authorization&lt;/strong&gt;: Checking user permissions.&lt;br&gt;
&lt;strong&gt;Logging&lt;/strong&gt;: Recording details about requests and responses.&lt;br&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Catching and managing exceptions.&lt;/p&gt;

&lt;p&gt;Using middleware can save you significant time and effort by centralizing and simplifying these common tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz13mhilaa7bdf1fylns2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz13mhilaa7bdf1fylns2.png" alt="Image description" width="249" height="385"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Middleware?
&lt;/h2&gt;

&lt;p&gt;Middleware is a piece of code (or a component) that sits in the HTTP request-response pipeline. It has access to all incoming requests and outgoing responses, allowing it to process or manipulate them. Middleware can log information, modify requests/responses, or even terminate requests early.&lt;/p&gt;
&lt;h2&gt;
  
  
  Common Examples of Middleware
&lt;/h2&gt;

&lt;p&gt;Some commonly used middleware includes:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Authentication Middleware&lt;/strong&gt;: Verifies user identity.&lt;br&gt;
&lt;strong&gt;Authorization Middleware&lt;/strong&gt;: Checks user permissions.&lt;br&gt;
&lt;strong&gt;Logging Middleware&lt;/strong&gt;: Logs details about requests and responses.&lt;br&gt;
&lt;strong&gt;Exception Handling Middleware&lt;/strong&gt;: Catches and handles exceptions.&lt;br&gt;
&lt;strong&gt;Static Files Middleware&lt;/strong&gt;: Serves static files like images, CSS, and JavaScript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // Add custom middleware
        app.Use(async (context, next) =&amp;gt;
        {
            await context.Response.WriteAsync("Hello from Middleware");
            await next(); // Call the next middleware in the pipeline
        });

        // Other middleware registrations
        app.UseStaticFiles();
        app.UseAuthentication();
        app.UseMvc();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Custom Middleware
&lt;/h2&gt;

&lt;p&gt;Let’s walk through the process of adding a custom middleware that writes "Hello from Middleware" to every response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Add Custom Middleware&lt;/strong&gt;&lt;br&gt;
Open Startup.cs: This file configures your application's request pipeline.&lt;br&gt;
Modify the Configure Method: Add your custom middleware within the Configure method.&lt;/p&gt;

&lt;p&gt;I hope this helps you understand how to integrate custom middleware into your application. Happy coding!`&lt;/p&gt;

</description>
      <category>aspdotnet</category>
      <category>middleware</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Recoil: Cheatsheet</title>
      <dc:creator>harshchandwani</dc:creator>
      <pubDate>Fri, 19 Jan 2024 12:58:27 +0000</pubDate>
      <link>https://forem.com/harshchandwani/recoil-cheatsheet-216k</link>
      <guid>https://forem.com/harshchandwani/recoil-cheatsheet-216k</guid>
      <description>&lt;p&gt;Hello Developers,&lt;br&gt;
This article was written when I was reading and learning to use Recoil for a project. I know about Redux, but Recoil is much more used in Open-Source and is gaining popularity, so this is a small article with shortcuts to use Recoil.&lt;/p&gt;

&lt;p&gt;For more information: &lt;a href="https://recoiljs.org/"&gt;https://recoiljs.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installation: &lt;br&gt;
For npm: &lt;code&gt;npm install recoil&lt;/code&gt;&lt;br&gt;
For CDN: &lt;code&gt;&amp;lt;script src = "https://cdn.jsdelivr.net/npm/recoil@0.0.11/umd/recoil.production.js"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To use Recoil, you need to make every component use Recoil.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.js
import React from 'react'
import { RecoilRoot } from 'recoil'
import ChildComponent from './component/ChildComponent'
const App = () =&amp;gt; {
  return (
      &amp;lt;RecoilRoot&amp;gt;
        &amp;lt;ChildComponent /&amp;gt;
      &amp;lt;/RecoilRoot&amp;gt;   
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Atoms are the smallest units of state in Recoil, serving as fundamental variables. This article guides you through creating and using an Atom, providing a clean structure for managing state variables within React components.&lt;/p&gt;

&lt;p&gt;Setting Up Your Atom:&lt;br&gt;
Organize your state-related code in a 'utils' file for better project structure management.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Atom.js
import { atom } from "recoil"
export const counterAtom = atom({
    key: 'counterAtom', //A unique identifier for the Atom, ensuring uniqueness across your application.
    default: '12' //The initial value of the Atom, is set to '12'. Adjust based on your desired initial state.
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, as you have created an atom or variables, how to access them?&lt;br&gt;
There are multiple ways.&lt;br&gt;
Let's go🚀&lt;br&gt;
&lt;/p&gt;

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

export const Component = () =&amp;gt; {
  const xAtom = useRecoilValue(xAtom); //only access to value
  const [yAtom, setyAtom] = useRecoilState(yAtom); //same as useState
  const setZAtom = useSetRecoilState(zAtom); //on set function, no access to the current value, but can change using set variable
  return (
    &amp;lt;&amp;gt;
      &amp;lt;p&amp;gt;xAtom&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setyAtom(yAtom+ 1)}&amp;gt;yAtom&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setZAtom(c + 1)}&amp;gt;zAtom&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But what if I want to combine two atoms?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One way is to do that in component [useMemo]&lt;/li&gt;
&lt;li&gt;Other is to use a selector
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Atom.js
import { atom, selector } from "recoil"
export const xAtom = atom({
    key: 'xAtom',
    default: 10
})
export const yAtom = atom({
    key: 'yAtom',
    default: 11
})

export const sum = selector({
    key: 'sum',
    get: ({get}) =&amp;gt; {
        return (
            xAtom.xAtom + 
            yAtom.yAtom
        )
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to accept and store backend calls&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { atom, selector } from "recoil"
import axios from "axios";
export const information = atom({
    key: 'information',
    default: selector({
        key: "informationSelector", 
        get: async () =&amp;gt; {
            const res = await axios.get("somerandomsite");
            return res.data
        }
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What is Atom Family&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you are making Todo, how will store multiple todo, creating different atom for different todo?&lt;/li&gt;
&lt;li&gt;No, for this Atom Family is here&lt;/li&gt;
&lt;li&gt;Let's have an example
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Atom.js
import { atomFamily } from "recoil"
const TODOS = [{
    id: 1,
    title: "Go to GYM",
    description: "Hit the gym"
}, {
    id: 2,
    title: "Go to Office",
    description: "Code something new"
}]

export const todoAtomFamily = atomFamily({
    key: "todoAtomFamily",
    default: id =&amp;gt; {
        return TODOS.find(x =&amp;gt; x.id === id)
    },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.js
import {todoAtomFamily } from './Atom'
const App = () =&amp;gt; {
  return (
      &amp;lt;RecoilRoot&amp;gt;
        &amp;lt;Main /&amp;gt;
      &amp;lt;/RecoilRoot&amp;gt;   
  )
}
const Main = () =&amp;gt; {
      &amp;lt;Todo id={1} /&amp;gt;
  )
}
function Todo({id}){
  const currentTodo = useRecoilValue(todoAtomFamily(id));
  return (
    &amp;lt;&amp;gt;
      {currentTodo.title}
      {currentTodo.description}
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Selector Family&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Atom.js
import { atomFamily, selectorFamily } from "recoil";
import axios from "axios";

export const todosAtomFamily = atomFamily({
  key: 'todosAtomFamily',
  default: selectorFamily({
    key: "todoSelectorFamily",
    get: (id) =&amp;gt; async ({get}) =&amp;gt; {
      const res = await axios.get(`https://sum-server.100xdevs.com/todo?id=${id}`);
      return res.data.todo;
    },
  })
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//App.js
const [todo, setTodo] = useRecoilState(todosAtomFamily(id));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all about it. Hope you get an idea of what recoil is and how to use it.&lt;br&gt;
I know the Recoil selector family is a little difficult and hard to understand, but it also used less.&lt;/p&gt;

&lt;p&gt;Thanks&lt;br&gt;
Harsh Chandwani&lt;br&gt;
Follow for more: &lt;a href="https://twitter.com/heyy_harshh"&gt;https://twitter.com/heyy_harshh&lt;/a&gt; &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>Understanding Middlewares</title>
      <dc:creator>harshchandwani</dc:creator>
      <pubDate>Wed, 27 Dec 2023 05:19:57 +0000</pubDate>
      <link>https://forem.com/harshchandwani/understanding-middlewares-508j</link>
      <guid>https://forem.com/harshchandwani/understanding-middlewares-508j</guid>
      <description>&lt;p&gt;Hello Coders! Hope you are getting no bugs in the code, today, I want to share a little bit about what I am learning in MIDDLEWARES, as the name suggests, it is something that comes into the middle. It is the pre-computing of something. Let's take an example and understand.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require("express");
const app = express();
function userMiddleware(req, res, next) {
    const username = req.headers.username;
    const password = req.headers.password;

    if (username !== "carlover" || password !== "carpassion") {
        res.status(403).json({
            msg: "User doesn't exist",
        });
        return;
    } else {
        next();
    }
}
function carMiddleware(req, res, next) {
    const carId = req.query.carId;

    if (carId !== "sedan" &amp;amp;&amp;amp; carId !== "suv") {
        res.status(411).json({
            msg: "Wrong car type",
        });
        return;
    } else {
        next();
    }
}
app.get("/car-checkup", userMiddleware, carMiddleware, function (req, res) {
    // do health checks here
    const carId = req.query.carId;

    //Do something with the car here
    res.json({
        msg: `Your ${carId} is in great condition!`
    });
});

app.listen(3000, () =&amp;gt; {
    console.log("Ready");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have 2 functions, userMiddleware and carMiddleware, &lt;br&gt;
and we are calling the functions in the app.get, why because we want to pre-check the same as we will only get into the 3rd function when the 2nd function is successfully computed and the 2nd will only compute when the 1st will be successfully computed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But where do we use Middlewares?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authentication: Middlewares can validate user credentials, ensuring only authorized users access protected routes, and enhancing application security.&lt;/p&gt;

&lt;p&gt;Logging: Implementing logging middleware helps track requests, errors, and user activities, aiding in debugging and performance monitoring.&lt;/p&gt;

&lt;p&gt;Data Validation: Middlewares validates incoming data, ensuring it adheres to specified formats or rules, preventing invalid or malicious inputs from impacting the application.&lt;/p&gt;

&lt;p&gt;CORS Handling: Middleware can manage Cross-Origin Resource Sharing (CORS), permitting or restricting resource access based on defined policies, and enhancing web application interoperability.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
