<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ibrahim Khalil on Medium]]></title>
        <description><![CDATA[Stories by Ibrahim Khalil on Medium]]></description>
        <link>https://medium.com/@ibrahimkhalil_6554?source=rss-d61b872da31f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zhPW-K5um2M5cZ7eMSnI0g.jpeg</url>
            <title>Stories by Ibrahim Khalil on Medium</title>
            <link>https://medium.com/@ibrahimkhalil_6554?source=rss-d61b872da31f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 02:35:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ibrahimkhalil_6554/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Installing DaisyUI in a React.js Project with Vite]]></title>
            <link>https://medium.com/@ibrahimkhalil_6554/installing-daisyui-in-a-react-js-project-with-vite-f2f71900f094?source=rss-d61b872da31f------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2f71900f094</guid>
            <category><![CDATA[daisyui]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[tailwind-css]]></category>
            <dc:creator><![CDATA[Ibrahim Khalil]]></dc:creator>
            <pubDate>Sun, 17 Nov 2024 11:11:52 GMT</pubDate>
            <atom:updated>2024-11-17T11:11:52.397Z</atom:updated>
            <content:encoded><![CDATA[<p>DaisyUI is a highly customizable and feature-rich Tailwind CSS plugin that simplifies building user interfaces with predefined, accessible, and visually appealing components. If you’re using <strong>React.js</strong> with <strong>Vite</strong>, integrating DaisyUI into your project is a breeze. Here’s a step-by-step guide to get you started.</p><h3>Step 1: Set Up a React Project with Vite</h3><p>Before installing DaisyUI, ensure you have a React project created using Vite.</p><ol><li><strong>Install Vite and Create a React Project</strong>: Run the following commands:</li></ol><pre>npm create vite@latest my-daisyui-app<br>cd my-daisyui-app<br>npm install</pre><p>2. During the setup, choose React or React + TypeScript as the framework/template.</p><h3>Step 2: Install Tailwind CSS</h3><p>DaisyUI is a plugin for Tailwind CSS, so you’ll need to set up Tailwind CSS first.</p><ol><li><strong>Install Tailwind CSS and PostCSS</strong>:</li></ol><pre>npm install -D tailwindcss postcss autoprefixer<br>npx tailwindcss init</pre><p><strong>2. Configure Tailwind CSS</strong>: Add the following content to your tailwind.config.js file:</p><pre>/** @type {import(&#39;tailwindcss&#39;).Config} */<br>module.exports = {<br>  content: [&#39;./index.html&#39;, &#39;./src/**/*.{js,jsx,ts,tsx}&#39;],<br>  theme: {<br>    extend: {},<br>  },<br>  plugins: [],<br>};</pre><p>3. <strong>Add Tailwind CSS Directives</strong>: In the src/index.css file (or create one if it doesn’t exist), add the following:</p><pre>@tailwind base;<br>@tailwind components;<br>@tailwind utilities;</pre><h3>Step 3: Install DaisyUI</h3><ol><li>Install DaisyUI: Run the following command:</li></ol><pre>npm install daisyui</pre><p><strong>2. Add DaisyUI to Tailwind Config</strong>: Update the plugins array in tailwind.config.js to include DaisyUI:</p><pre>plugins: [require(&#39;daisyui&#39;)],</pre><h3>Step 4: Customize DaisyUI (Optional)</h3><p>You can customize DaisyUI’s themes in your tailwind.config.js. For example:</p><pre>module.exports = {<br>  content: [&#39;./index.html&#39;, &#39;./src/**/*.{js,jsx,ts,tsx}&#39;],<br>  theme: {<br>    extend: {},<br>  },<br>  plugins: [require(&#39;daisyui&#39;)],<br>  daisyui: {<br>    themes: [&#39;light&#39;, &#39;dark&#39;, &#39;cupcake&#39;], // List of themes you want to use<br>  },<br>};</pre><h3>Step 5: Use DaisyUI Components</h3><p>DaisyUI provides prebuilt components that can be used directly in your JSX files.</p><ol><li>Open a React component file (e.g., App.jsx).</li><li>Use a DaisyUI component:</li></ol><pre>const App = () =&gt; {<br>  return (<br>    &lt;div className=&quot;p-6&quot;&gt;<br>      &lt;h1 className=&quot;text-3xl font-bold text-center&quot;&gt;Hello DaisyUI!&lt;/h1&gt;<br>      &lt;button className=&quot;btn btn-primary mt-4&quot;&gt;Click Me&lt;/button&gt;<br>    &lt;/div&gt;<br>  );<br>};<br><br>export default App;</pre><h3>Step 6: Run the Application</h3><p>Run your project to see DaisyUI in action:</p><pre>npm run dev</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2f71900f094" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving the “CORS Policy Blocked” Error in Web Applications]]></title>
            <link>https://medium.com/@ibrahimkhalil_6554/solving-the-cors-policy-blocked-error-in-web-applications-2f30ce796914?source=rss-d61b872da31f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f30ce796914</guid>
            <category><![CDATA[vercel]]></category>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[cors]]></category>
            <dc:creator><![CDATA[Ibrahim Khalil]]></dc:creator>
            <pubDate>Mon, 28 Oct 2024 06:23:23 GMT</pubDate>
            <atom:updated>2024-10-28T06:23:23.006Z</atom:updated>
            <content:encoded><![CDATA[<p>If you’re working on a web application with separate frontend and backend services, chances are you’ve encountered a <strong>CORS</strong> (Cross-Origin Resource Sharing) issue. One common error message looks like this:</p><blockquote>Access to fetch at &#39;https://your-backend-url.com/api/some-resource&#39; from origin &#39;https://your-frontend-url.com&#39; has been blocked by CORS policy: No &#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.</blockquote><p>This error usually occurs when your frontend (served from one domain) tries to make requests to your backend (served from another domain), and the backend doesn’t allow cross-origin requests from your frontend. In this article, I’ll walk you through what this error means, why it happens, and how you can fix it.</p><h3>Why You’re Seeing This Error</h3><p>Let’s break down the common error message:</p><blockquote>Access to fetch at ‘https://your-backend-url.com/api/some-resource&#39; from origin ‘https://your-frontend-url.com&#39; has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.</blockquote><ul><li><strong>Origin</strong>: The domain from which the request originates (in this case, your frontend: <a href="https://your-frontend-url.com)./">https://your-frontend-url.com).</a></li><li><strong>Request URL</strong>: The URL of your backend resource (e.g., <a href="https://your-backend-url.com/api/some-resource).">https://your-backend-url.com/api/some-resource).</a></li><li><strong>No ‘Access-Control-Allow-Origin’ header</strong>: This means that the backend’s response is missing the header that tells the browser which domains are allowed to access the resource.</li></ul><p>Essentially, your browser is blocking the request because the backend didn’t specify that the frontend domain (https://your-frontend-url.com) is allowed to access its resources.</p><h3>How to Fix CORS Issues</h3><p>The solution to this error involves configuring your backend to explicitly allow cross-origin requests from your frontend.</p><h4>Step 1: Installing CORS Middleware (For Express Apps)</h4><p>If you’re using <strong>Express</strong> for your backend, the easiest way to fix this is by using the cors middleware. Here&#39;s how to install it:</p><pre>npm install cors</pre><h4>Step 2: Configuring CORS</h4><p>Once you have the cors package installed, you can configure it to allow requests from your frontend domain.</p><p>In your backend’s entry point (usually index.js or app.js), add the following:</p><pre>const express = require(&#39;express&#39;);<br>const cors = require(&#39;cors&#39;);<br>const app = express();<br><br>function setCorsHeaders(req, res, next) {<br>  res.setHeader(&quot;Access-Control-Allow-Origin&quot;, &quot;*&quot;);<br>  res.setHeader(<br>    &quot;Access-Control-Allow-Methods&quot;,<br>    &quot;GET, POST, PUT, PATCH, DELETE, OPTIONS&quot;<br>  );<br>  res.setHeader(&quot;Access-Control-Allow-Headers&quot;, &quot;Content-Type, Authorization&quot;);<br><br>  if (req.method === &quot;OPTIONS&quot;) {<br>    // Respond to preflight requests<br>    return res.status(200).end();<br>  }<br><br>  next();<br>}<br><br>app.use(setCorsHeaders);<br><br>// Example route<br>app.get(&#39;/api/some-resource&#39;, (req, res) =&gt; {<br>  res.json({ message: &#39;Hello, CORS!&#39; });<br>});<br><br>// Start server<br>app.listen(3000, () =&gt; {<br>  console.log(&#39;Server running on port 3000&#39;);<br>});</pre><h4>Step 3: Redeploy Your Backend</h4><p>Once you’ve added the CORS configuration to your backend, redeploy it. For example, if you’re using <strong>Vercel</strong>, you can redeploy the updated backend, and it should now allow cross-origin requests from your frontend.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f30ce796914" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optimizing MongoDB API Development: Why You Should Declare Collections Outside Route Handlers]]></title>
            <link>https://medium.com/@ibrahimkhalil_6554/optimizing-mongodb-api-development-why-you-should-declare-collections-outside-route-handlers-741331629154?source=rss-d61b872da31f------2</link>
            <guid isPermaLink="false">https://medium.com/p/741331629154</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[mongodb]]></category>
            <dc:creator><![CDATA[Ibrahim Khalil]]></dc:creator>
            <pubDate>Tue, 22 Oct 2024 06:41:37 GMT</pubDate>
            <atom:updated>2024-10-22T06:41:37.813Z</atom:updated>
            <content:encoded><![CDATA[<p>When developing an API with MongoDB, a common question arises: should we declare the database collection inside each route handler or outside for shared use? While both approaches can work, declaring the collection <strong>outside</strong> the routing methods is generally a better practice. In this article, we’ll explore why this is the case and how it can benefit your API development.</p><h4>1. Code Reusability and DRY Principle</h4><p>Declaring the collection outside the route handlers ensures that you’re not repeating the same code across every route. This adheres to the <strong>DRY</strong> (Don’t Repeat Yourself) principle, leading to cleaner and more maintainable code. When you avoid redundancy, any future modifications — like database configuration changes — can be done in one place rather than across multiple route handlers.</p><h4>2. Performance Improvements</h4><p>If you declare the collection within each method, you’re essentially fetching or reconnecting to the database in every single request, which adds unnecessary overhead. When the collection is declared once outside the handlers, this overhead is avoided, and the API can perform slightly better, especially at scale.</p><h4>3. Centralized Error Handling</h4><p>When you declare the collection once in a shared space, you can centralize error handling related to database connections. This reduces complexity and ensures that any potential errors in accessing the database are handled consistently, making debugging more straightforward.</p><h4>4. Scalability and Flexibility</h4><p>Centralizing the collection declaration makes it easier to manage and scale your application. If you need to add middleware, logging, or change configurations (e.g., for sharding or replica sets), you only need to modify one part of your code rather than updating each route handler individually.</p><h3>Example Code</h3><p>Here’s how you can structure your MongoDB API with the collection declared outside the route handlers:</p><pre>const express = require(&#39;express&#39;);<br>const { MongoClient } = require(&#39;mongodb&#39;);<br>const app = express();<br><br>const client = new MongoClient(&#39;mongodb://localhost:27017&#39;, { useUnifiedTopology: true });<br>let db, usersCollection;<br><br>client.connect().then(() =&gt; {<br>  db = client.db(&#39;mydatabase&#39;);<br>  usersCollection = db.collection(&#39;users&#39;);<br>});<br><br>app.get(&#39;/users&#39;, async (req, res) =&gt; {<br>  const users = await usersCollection.find().toArray();<br>  res.json(users);<br>});<br><br>app.post(&#39;/users&#39;, async (req, res) =&gt; {<br>  const newUser = req.body;<br>  await usersCollection.insertOne(newUser);<br>  res.status(201).send(&#39;User created&#39;);<br>});<br><br>app.listen(3000, () =&gt; console.log(&#39;Server running on port 3000&#39;));</pre><h3>Why This Approach is Better</h3><p>In the example above, the usersCollection is declared once, then reused in multiple route handlers. This keeps the codebase cleaner and ensures that any changes to the collection configuration only need to be made once.</p><h3>Pitfalls of Declaring the Collection Inside Each Method</h3><p>If you declare the collection inside each method, your code may look something like this:</p><pre>app.get(&#39;/users&#39;, async (req, res) =&gt; {<br>  const db = client.db(&#39;mydatabase&#39;);<br>  const usersCollection = db.collection(&#39;users&#39;);<br>  const users = await usersCollection.find().toArray();<br>  res.json(users);<br>});<br><br>app.post(&#39;/users&#39;, async (req, res) =&gt; {<br>  const db = client.db(&#39;mydatabase&#39;);<br>  const usersCollection = db.collection(&#39;users&#39;);<br>  const newUser = req.body;<br>  await usersCollection.insertOne(newUser);<br>  res.status(201).send(&#39;User created&#39;);<br>});</pre><p>While this works, it introduces unnecessary repetition and complicates maintenance. Every route handler repeats the process of fetching the collection, which makes it harder to manage if changes are needed. This also makes error handling more dispersed, increasing the likelihood of inconsistencies.</p><h3>Conclusion</h3><p>Declaring the MongoDB collection outside the routing methods is a best practice for API development. It improves code maintainability, boosts performance by reducing redundant operations, and centralizes error handling. This approach ensures that your code remains scalable and easy to modify as your application grows.</p><p>By following this practice, you can build more efficient, maintainable, and scalable APIs that are easier to manage in the long run.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=741331629154" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>