<?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: Abhishek shah</title>
    <description>The latest articles on DEV Community by Abhishek shah (@shahiscoding).</description>
    <link>https://dev.to/shahiscoding</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%2F653866%2Ffb4eb4f2-3258-4ddb-ad5e-74163f78f918.jpeg</url>
      <title>DEV Community: Abhishek shah</title>
      <link>https://dev.to/shahiscoding</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shahiscoding"/>
    <language>en</language>
    <item>
      <title>HacktoberFest Experience as Contributor</title>
      <dc:creator>Abhishek shah</dc:creator>
      <pubDate>Wed, 25 Oct 2023 17:53:00 +0000</pubDate>
      <link>https://dev.to/shahiscoding/placeholder-contributor-1cl9</link>
      <guid>https://dev.to/shahiscoding/placeholder-contributor-1cl9</guid>
      <description>&lt;p&gt;&lt;strong&gt;Intro&lt;/strong&gt;&lt;br&gt;
hi,This is the first time i have participated on open source hackathon . For the past few months i have been trying to start open source contributions , hacktoberfest2023 provided with and opportunity to start open source journey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Highs and Lows&lt;/strong&gt;&lt;br&gt;
The biggest issue is finding new repository which is actively maintained and contributing to it. I got a better understanding of using GitHub and how to contribute &lt;/p&gt;

&lt;p&gt;So any contirbutor looking taking part in Hacktoberfest.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;always check the last commit merged to get idea if mainter is active or not&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;before jumping to issue go through the contributor.md file and work on issue accordingly some repos have strict rules others don't.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These two points will save u from making PR that won't get merged.&lt;/p&gt;

&lt;p&gt;Thank you&lt;/p&gt;

</description>
      <category>hack23contributor</category>
      <category>hacktoberfest</category>
      <category>opensource</category>
      <category>beginners</category>
    </item>
    <item>
      <title>BINARY SEARCH</title>
      <dc:creator>Abhishek shah</dc:creator>
      <pubDate>Fri, 24 Sep 2021 12:47:17 +0000</pubDate>
      <link>https://dev.to/shahiscoding/binary-search-45jp</link>
      <guid>https://dev.to/shahiscoding/binary-search-45jp</guid>
      <description>&lt;h1&gt;
  
  
  What is Binary Search and why to use it?
&lt;/h1&gt;

&lt;p&gt;Binary Search is a search algorithm. It is used to efficiently search   for position of a particular number in a sorted array, character in an sorted String. It can also be used to search for first occurrences of a element or last occurrence. Most importantly it can only be used  in case of sorted arrays or string.&lt;/p&gt;

&lt;h1&gt;
  
  
  Logic
&lt;/h1&gt;

&lt;p&gt;In the start we calculate the middle position as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int s = 0, e = the length of array or string.
int mid = (s+e)/2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now after we have mid we have 3 case at hand.check if the element at the middle is greater or smaller than the element we are searching for (let say its X).&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 1: &lt;code&gt;arr[mid]&amp;gt;X&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Then that means the element after the mid are also greater than X. So we can reduce the array in search from &lt;code&gt;s to mid&lt;/code&gt;. So, we do &lt;code&gt;e=mid-1&lt;/code&gt;;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 2: &lt;code&gt;arr[mid]&amp;lt;X&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Then that means the element before the mid are also smaller than X. So we can reduce the array in search from &lt;code&gt;mid + 1 to e&lt;/code&gt;. So, we do &lt;code&gt;s=mid+1&lt;/code&gt;;&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 3: &lt;code&gt;arr[mid]==X&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When we finally got the position of X.&lt;/p&gt;

&lt;p&gt;Here is a code to show how it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int binarySort(int arr[],int X){
  int arr=[1,2,3,4,5,6,7,8,9]; // A sorted Array of length n
  int s = 0,e = n-1;

  while(s&amp;lt;=e){
   int mid = (s+e)/2;
   if(arr[mid] &amp;gt; X) e = mid - 1;
   else if (arr[mid] &amp;lt; X) s = mid + 1;
   else return mid;
  }
  return -1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To Find First Occurrence or Last Occurrence
&lt;/h2&gt;

&lt;p&gt;When we find the position of X we have to new condition for First Occurrence and Last Occurrence.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Occurrence:
&lt;/h3&gt;

&lt;p&gt;Check if there is the same element present at mid - 1 position if yes then you would have to decrease the search space. in the case of First Occurrence or mid + 1 position in case of Last Occurrence&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   else{
     if(arr[mid-1] != arr[mid] or mid == 0)
          return mid;
     else 
          e = mid - 1;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Last Occurrence:
&lt;/h3&gt;

&lt;p&gt;Check if there is the same element present at mid + 1 position if yes then you would have to decrease the search space&lt;br&gt;
by making s = mid+1;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   else{
     if(arr[mid+1] != arr[mid] or mid == n-1)
          return mid;
     else 
          s = mid + 1;

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

&lt;/div&gt;



&lt;p&gt;If you have anything to add do suggest in the comments.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>algorithms</category>
      <category>beginners</category>
      <category>cpp</category>
    </item>
    <item>
      <title>Using Socket.io in express-generator.</title>
      <dc:creator>Abhishek shah</dc:creator>
      <pubDate>Mon, 20 Sep 2021 09:34:06 +0000</pubDate>
      <link>https://dev.to/shahiscoding/using-socket-io-in-express-generator-4pkp</link>
      <guid>https://dev.to/shahiscoding/using-socket-io-in-express-generator-4pkp</guid>
      <description>&lt;p&gt;Socket.io has documentation on how to use it with express but it a bit tricky to use socket.io with express-generator because the server is created in www file and the io is created in app or server but the use of io is in the router .So how can you use socket.io in express-generator ?&lt;br&gt;&lt;br&gt;
           Its very simple you can create a new file as mysocket and create instance of io over there and export it.&lt;/p&gt;

&lt;h2&gt;
  
  
  In the mysocket file.
&lt;/h2&gt;

&lt;blockquote&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let socketIo = require('socket.io');
let io = socketIo();
let socketApi = {};
socketApi.io = io;
module.exports = socketApi;
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  In the www file
&lt;/h2&gt;

&lt;blockquote&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var server = http.createServer(app);
socketAPI.io.attach(server);
&lt;/code&gt;&lt;/pre&gt;

&lt;/blockquote&gt;

&lt;p&gt;So, you can import io from mysocket file into the router and use it.&lt;br&gt;
If you have anything to add you can leave a comment.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>node</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Handling CORS in Axios and Socket.io</title>
      <dc:creator>Abhishek shah</dc:creator>
      <pubDate>Sun, 19 Sep 2021 11:06:58 +0000</pubDate>
      <link>https://dev.to/shahiscoding/handling-cors-in-axions-and-socket-io-1l0a</link>
      <guid>https://dev.to/shahiscoding/handling-cors-in-axions-and-socket-io-1l0a</guid>
      <description>&lt;h1&gt;
  
  
  What is CORS??
&lt;/h1&gt;

&lt;p&gt;CORS stand for Cross-Origin Resource Sharing. It's a way for the server to check if the client the server is communicating with is actually a permitted client to use the server. Before the browsers  send the actual request for any operation the client sends a preflight request with a header where Origin is  set to  its base URL and the server replies with a &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; in response header.&lt;br&gt;
                If it's value is a wildcard('*') or the base URL matches the Origin set in the request header only then the actual request is made else you get a CORS error. This has been shown in the picture below especially focusing on the &lt;code&gt;Origin&lt;/code&gt; values in the request header and &lt;code&gt;Access-Control-Allow-Origin&lt;/code&gt; in the response header.&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%2Fuploads%2Farticles%2Fkniit40nvscz258vifv8.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%2Fuploads%2Farticles%2Fkniit40nvscz258vifv8.png" alt="Header exchanges after a request is made"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you are getting a CORS error go to inspect mode by pressing F12 and go to network and see in the header of any file , you will find a request and response header. You can add other headers to it at &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS" rel="noopener noreferrer"&gt;CORS&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now Lets see how to handle the CORS error if you are using &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://axios-http.com/docs/intro" rel="noopener noreferrer"&gt;Axios&lt;/a&gt; 
You can use &lt;a href="https://www.npmjs.com/package/cors" rel="noopener noreferrer"&gt;CORS npm package&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var express = require('express')
var cors = require('cors')
var app = express()

var whitelist = ['http://example1.com', 'http://example2.com']
var corsOptions = {
  origin: function (origin, callback) {
    if (whitelist.indexOf(origin) !== -1) {
      callback(null, true)
    } else {
      callback(new Error('Not allowed by CORS'))
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Here you can directly do an &lt;code&gt;app(cors(corsOptions))&lt;/code&gt; before the routers or you can add  &lt;code&gt;cors(corsOptions)&lt;/code&gt; in the (req,res,next) part.&lt;/p&gt;

&lt;p&gt;2 &lt;a href="https://socket.io/" rel="noopener noreferrer"&gt;Socket.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In socket.io you have to add cors while creating io.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const io = require("socket.io")(server, {
  cors: {
    origin: "https://example.com",
    methods: ["GET", "POST"]
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have anything to add do comment and share your views.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>node</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
