<?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: Riky Fahri Hasibuan</title>
    <description>The latest articles on Forem by Riky Fahri Hasibuan (@codenoun).</description>
    <link>https://forem.com/codenoun</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%2F585299%2F730cb9f5-6e1a-4aca-8ed3-9a043e9db0ac.png</url>
      <title>Forem: Riky Fahri Hasibuan</title>
      <link>https://forem.com/codenoun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/codenoun"/>
    <language>en</language>
    <item>
      <title>How To Secure Apache with Let's Encrypt</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Fri, 04 Oct 2024 03:06:33 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-secure-apache-with-lets-encrypt-l52</link>
      <guid>https://forem.com/codenoun/how-to-secure-apache-with-lets-encrypt-l52</guid>
      <description>&lt;p&gt;Securing your website with SSL (Secure Socket Layer) is an essential step to protect users’ data and build trust. One of the easiest and most cost-effective ways to achieve this is by using &lt;a href="https://letsencrypt.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;Let’s Encrypt SSL&lt;/strong&gt;&lt;/a&gt;, a free, automated, and open certificate authority that provides SSL certificates.&lt;/p&gt;

&lt;p&gt;In this guide, you’ll learn &lt;a href="https://codenoun.com/secure-apache-with-lets-encrypt-ssl-ubuntu/" rel="noopener noreferrer"&gt;how to secure Apache on Ubuntu with Let’s Encrypt SSL&lt;/a&gt; and configure Apache for SSL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before you proceed, ensure that the following are in place:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A domain name&lt;/strong&gt; pointing to your Ubuntu server (e.g., &lt;code&gt;example.com&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A non-root user&lt;/strong&gt; with sudo privileges&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apache web server&lt;/strong&gt; installed on Ubuntu&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ports 80 and 443&lt;/strong&gt; opened on your firewall&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Install Apache on Ubuntu
&lt;/h2&gt;

&lt;p&gt;First, you need to install Apache if it's not already installed. Run the following commands to install and enable Apache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;apache2
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start apache2
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To verify that Apache is installed and running, visit your server’s IP address in a web browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://your-server-ip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the default Apache page, your installation is successful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Install Certbot for Let’s Encrypt
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://certbot.eff.org/" rel="noopener noreferrer"&gt;&lt;strong&gt;Certbot&lt;/strong&gt;&lt;/a&gt; is a command-line tool that simplifies the process of obtaining SSL certificates from Let’s Encrypt. Certbot automatically configures Apache with the new SSL certificate. Install Certbot by running the following commands:&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Certbot and Apache plugin
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;certbot python3-certbot-apache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command installs Certbot and the Apache plugin, which automates the SSL configuration process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Obtain Let’s Encrypt SSL Certificate
&lt;/h2&gt;

&lt;p&gt;With Certbot installed, you can now obtain your SSL certificate. Certbot will request certificates, configure your Apache server, and automatically redirect HTTP traffic to HTTPS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Run the following command to obtain your SSL certificate
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot &lt;span class="nt"&gt;--apache&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; example.com &lt;span class="nt"&gt;-d&lt;/span&gt; www.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Replace &lt;code&gt;example.com&lt;/code&gt; with your actual domain name.&lt;/li&gt;
&lt;li&gt;Certbot will prompt you to enter your email for recovery purposes and agree to the terms of service.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Certbot will ask if you want to redirect HTTP to HTTPS. Choose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Option 2: Redirect - Make all requests redirect to secure HTTPS access.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this process, Certbot will obtain and install the SSL certificates, and your Apache configuration will be updated automatically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/example.com/fullchain.pem
Key is saved at: /etc/letsencrypt/live/example.com/privkey.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The certificate will be valid for 90 days, and Certbot will automatically renew it (covered in Step 5).&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Apache SSL Configuration
&lt;/h2&gt;

&lt;p&gt;Once Let’s Encrypt SSL is installed, it’s important to verify that your Apache SSL configuration is correct. Apache uses the &lt;code&gt;.conf&lt;/code&gt; files located in &lt;code&gt;/etc/apache2/sites-available/&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check Apache Virtual Hosts
&lt;/h3&gt;

&lt;p&gt;You can verify the configuration for your domain by viewing the Apache configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/apache2/sites-available/example.com.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure the following SSL-related lines are included in your Virtual Host configuration for port 443:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;VirtualHost &lt;span class="k"&gt;*&lt;/span&gt;:443&amp;gt;
    ServerName example.com
    ServerAlias www.example.com

    DocumentRoot /var/www/html

    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Enable SSL Module
&lt;/h3&gt;

&lt;p&gt;If not already enabled, enable the SSL module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;a2enmod ssl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After editing the Apache configuration, restart Apache for the changes to take effect:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart apache2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Redirect HTTP to HTTPS
&lt;/h3&gt;

&lt;p&gt;Certbot should have automatically set up an HTTP to HTTPS redirect. If not, ensure that your configuration for port 80 includes the following redirect lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&amp;lt;VirtualHost &lt;span class="k"&gt;*&lt;/span&gt;:80&amp;gt;
    ServerName example.com
    ServerAlias www.example.com

    RewriteEngine On
    RewriteCond %&lt;span class="o"&gt;{&lt;/span&gt;HTTPS&lt;span class="o"&gt;}&lt;/span&gt; off
    RewriteRule ^/?&lt;span class="o"&gt;(&lt;/span&gt;.&lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; https://%&lt;span class="o"&gt;{&lt;/span&gt;SERVER_NAME&lt;span class="o"&gt;}&lt;/span&gt;/&lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;R&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;301,L]
&amp;lt;/VirtualHost&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration will ensure all HTTP traffic is automatically redirected to HTTPS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Set Up Automatic SSL Certificate Renewal
&lt;/h2&gt;

&lt;p&gt;Let’s Encrypt SSL certificates are valid for 90 days, but Certbot can automatically renew them for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check the Cron Job
&lt;/h3&gt;

&lt;p&gt;Certbot installs a cron job that automatically renews certificates and reloads Apache. You can verify the cron job by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status certbot.timer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Test Renewal Process
&lt;/h3&gt;

&lt;p&gt;To test the automatic renewal process, use the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;certbot renew &lt;span class="nt"&gt;--dry-run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the dry-run completes successfully, the automatic renewal process is set up correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Testing the SSL Configuration
&lt;/h2&gt;

&lt;p&gt;After the setup, it’s important to test your SSL configuration to ensure everything works as expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verify HTTPS Access
&lt;/h3&gt;

&lt;p&gt;Visit your domain via HTTPS to check that the certificate is correctly installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the padlock icon in your browser, indicating that the connection is secure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Test SSL Configuration
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;SSL Labs&lt;/strong&gt; to test your server's SSL configuration. Go to &lt;a href="https://www.ssllabs.com/ssltest/" rel="noopener noreferrer"&gt;SSL Labs Test Page&lt;/a&gt; and enter your domain name. This will give you a detailed report on your SSL setup, including security vulnerabilities and performance optimization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Securing Apache with Let’s Encrypt SSL on Ubuntu is a straightforward process, thanks to Certbot's automation. The steps above guide you through obtaining a free SSL certificate, configuring Apache for SSL, setting up automatic renewals, and testing the configuration. With &lt;strong&gt;Let’s Encrypt SSL&lt;/strong&gt;, your website is more secure, and you’ll instill greater confidence in your visitors by encrypting their data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands Cheat Sheet
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Command&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Description&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sudo apt install apache2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Installs the Apache web server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sudo apt install certbot&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Installs Certbot for obtaining SSL certificates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sudo certbot --apache&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Obtains and installs SSL certificate automatically&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sudo certbot renew --dry-run&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tests automatic renewal of SSL certificates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;sudo systemctl restart apache2&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restarts Apache to apply changes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By following this comprehensive guide, you can easily implement &lt;strong&gt;Let’s Encrypt SSL&lt;/strong&gt; and manage &lt;strong&gt;Apache SSL configuration&lt;/strong&gt; on Ubuntu. Always ensure that your SSL certificates are up-to-date to maintain security and compliance with modern web standards.&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Article
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://codenoun.com/how-to-configure-nginx-for-https/" rel="noopener noreferrer"&gt;&lt;strong&gt;How to Configure Nginx for HTTPS&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://codenoun.com/create-ssh-key-ubuntu-secure-server-access/" rel="noopener noreferrer"&gt;&lt;strong&gt;How to Create SSH Key on Ubuntu for Secure Server Access&lt;/strong&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>linux</category>
      <category>ubuntu</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>API Rate Limiting in Node.js</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Wed, 11 Sep 2024 19:14:18 +0000</pubDate>
      <link>https://forem.com/codenoun/api-rate-limiting-in-nodejs-2dae</link>
      <guid>https://forem.com/codenoun/api-rate-limiting-in-nodejs-2dae</guid>
      <description>&lt;p&gt;APIs form the backbone of modern web communication, and it's crucial to manage how often clients access them. Implementing rate limiting ensures your server remains responsive and secure by controlling the flow of requests to your API.&lt;/p&gt;

&lt;p&gt;This guide focuses on the key strategies for implementing API rate limiting in &lt;strong&gt;Node.js&lt;/strong&gt;, a widely used platform for building scalable web services.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is API Rate Limiting?
&lt;/h2&gt;

&lt;p&gt;API rate limiting restricts the number of requests a user or client can make to an API within a given timeframe. It's a safeguard against overuse and abuse, designed to ensure fair access to resources and maintain server health.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is API Rate Limiting Important?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DDoS Protection&lt;/strong&gt;: Limits the impact of Distributed Denial of Service (DDoS) attacks by reducing the number of requests from a single source.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved Server Performance&lt;/strong&gt;: Prevents server overload by distributing resources fairly among users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Better User Experience&lt;/strong&gt;: Ensures all users get timely responses by preventing misuse of the API.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices for API Rate Limiting in Node.js
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Implement Middleware
&lt;/h3&gt;

&lt;p&gt;Using middleware to manage rate limiting is both efficient and effective. The &lt;code&gt;express-rate-limit&lt;/code&gt; package is one popular tool for this in Node.js, especially when working with the Express framework. You can install the package by typing &lt;code&gt;npm i express-rate-limit&lt;/code&gt; in your console.&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;rateLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-rate-limit&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;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// 15 minutes&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Limit each IP to 100 requests per windowMs&lt;/span&gt;
  &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Too many requests from this IP, please try again after 15 minutes&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;limiter&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;windowMs&lt;/code&gt; sets a 15-minute window.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max&lt;/code&gt; limits each IP to 100 requests in that window.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message&lt;/code&gt; provides feedback when limits are exceeded.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using middleware like this ensures requests are filtered early in the process, saving server resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Use Redis for Distributed Systems
&lt;/h3&gt;

&lt;p&gt;For APIs running on multiple servers, rate limiting needs to be consistent across the entire system. Redis is often the go-to solution for shared storage in these cases. Combine &lt;code&gt;express-rate-limit&lt;/code&gt; with &lt;code&gt;rate-limit-redis&lt;/code&gt; for smooth implementation.&lt;/p&gt;

&lt;p&gt;You'll need to install the following packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;express&lt;/code&gt;: The web framework to create the API.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;redis&lt;/code&gt;: Communicate with Redis to track and store request counts.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;express-rate-limit&lt;/code&gt;: Middleware to handle rate limiting.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rate-limit-redis&lt;/code&gt;: Plugin to store rate limit data in Redis.
&lt;/li&gt;
&lt;/ul&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;RedisStore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rate-limit-redis&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;redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redis&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;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createClient&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;limiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;store&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;RedisStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&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;



&lt;p&gt;This setup ensures that request limits are maintained no matter which server handles the request, thanks to Redis acting as a central store. For the full explanation, You can find check out our article about &lt;strong&gt;&lt;a href="https://codenoun.com/api-rate-limiting-with-redis-nodejs/" rel="noopener noreferrer"&gt;how to implement API Rate Limiting with Redis and Node.js&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Add Limits for Different User Types
&lt;/h3&gt;

&lt;p&gt;Different users have different needs. A common approach is to allow more requests for premium users while limiting requests for those on free plans.&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;rateLimit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-rate-limit&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;freeLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Free-tier users get 50 requests per window&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;premiumLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rateLimit&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;windowMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Premium users get 1000 requests per window&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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/free/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;freeLimiter&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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/api/premium/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;premiumLimiter&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 helps balance user experience based on the service level.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Dynamic Rate Limiting
&lt;/h3&gt;

&lt;p&gt;Static rate limits may not always reflect user needs. Some users may require higher limits at specific times, which can be handled by dynamically adjusting limits based on usage patterns.&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;let&lt;/span&gt; &lt;span class="nx"&gt;userRequestCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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="nf"&gt;use&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="nx"&gt;next&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userRequestCount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rate limit exceeded, please try again later.&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This flexibility allows your API to respond intelligently to varying usage scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Communicate with Retry Headers
&lt;/h3&gt;

&lt;p&gt;Users appreciate knowing when they can try again. By adding a &lt;code&gt;Retry-After&lt;/code&gt; header to rate-limited responses, you can guide users on how long to wait before making another request.&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="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Retry-After&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 60 seconds&lt;/span&gt;
&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;429&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Too many requests, please try again later.&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 small step improves the overall user experience and reduces frustration for clients interacting with your API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Monitoring and Fine-Tuning
&lt;/h2&gt;

&lt;p&gt;Rate limiting should be continuously monitored and adjusted based on real-world usage patterns. Tracking key metrics such as the number of rate limit violations, API response times, and user feedback will help you make informed adjustments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Metrics to Track
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limit Violations&lt;/strong&gt;: High numbers may indicate that the limits are too strict or that users require more flexibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server Performance&lt;/strong&gt;: Keeping an eye on response times can reveal if rate limiting has the desired effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User Feedback&lt;/strong&gt;: Feedback from API users can provide insights into whether rate limits are too restrictive or if changes are needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Monitoring tools such as &lt;a href="https://prometheus.io/" rel="noopener noreferrer"&gt;Prometheus&lt;/a&gt; and &lt;a href="https://grafana.com/" rel="noopener noreferrer"&gt;Grafana&lt;/a&gt; can provide real-time insights into how your rate limiting is performing and where adjustments may be needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;API rate limiting is necessary for managing traffic, protecting resources, and ensuring fair usage. By following these practices in Node.js, you can build a resilient system that balances security with user experience.&lt;/p&gt;

&lt;p&gt;Whether you're implementing basic limits or building dynamic systems that adjust in real time, effective rate limiting is an essential part of API management.&lt;/p&gt;

&lt;p&gt;For more insights and tutorials, visit &lt;a href="https://codenoun.com" rel="noopener noreferrer"&gt;CodeNoun&lt;/a&gt; and learn how to build scalable Node.js applications efficiently.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to Integrate Meilisearch with Node.js</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Sun, 01 Sep 2024 20:36:50 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-integrate-meilisearch-with-nodejs-7d</link>
      <guid>https://forem.com/codenoun/how-to-integrate-meilisearch-with-nodejs-7d</guid>
      <description>&lt;p&gt;As a Node.js developer, building applications that deliver fast and accurate search results is important. Users expect immediate and relevant responses, which can be challenging to implement, especially when dealing with large datasets.&lt;/p&gt;

&lt;p&gt;This is where Meilisearch comes in—a search engine built to handle these demands with ease.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Meilisearch?
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.meilisearch.com/" rel="noopener noreferrer"&gt;Meilisearch&lt;/a&gt; is an open-source search engine known for its speed and ease of use. It's designed to offer quick and relevant search results, making it a great fit for modern web applications.&lt;/p&gt;

&lt;p&gt;Unlike other search engines that can be complex to set up and maintain, Meilisearch offers a more straightforward approach, focusing on simplicity and efficiency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Features of Meilisearch:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blazing Speed:&lt;/strong&gt; Processes search queries in milliseconds, providing users with instant results.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Relevant Results:&lt;/strong&gt; Utilizes advanced algorithms to ensure search outcomes are highly pertinent to user queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy Integration:&lt;/strong&gt; Offers seamless integration with various platforms and languages, including Node.js.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customizable:&lt;/strong&gt; Allows developers to tailor search parameters according to specific application needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Typo Tolerance:&lt;/strong&gt; Handles misspelled queries gracefully, enhancing user satisfaction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Choose Meilisearch for Your Node.js Projects?
&lt;/h3&gt;

&lt;p&gt;Integrating Meilisearch into your Node.js applications brings numerous benefits that elevate the overall functionality and user engagement.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Setup:&lt;/strong&gt; Setting up Meilisearch with Node.js is straightforward, allowing you to implement robust search features without unnecessary complexity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; Efficiently handles growing datasets, making it suitable for applications expected to expand over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-Time Updates:&lt;/strong&gt; Reflects data changes instantly, ensuring users always access the most up-to-date information.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexible Configuration:&lt;/strong&gt; Supports various customization options to meet diverse search requirements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Strong Community Support:&lt;/strong&gt; Backed by an active community, providing extensive resources and assistance for developers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Full Tutorial on: &lt;a href="https://codenoun.com/meilisearch-node-js-integration/" rel="noopener noreferrer"&gt;How to Integrate Meilisearch with Node.js&lt;/a&gt;&lt;br&gt;
Telegram: &lt;a href="https://t.me/codenoun" rel="noopener noreferrer"&gt;https://t.me/codenoun&lt;/a&gt;&lt;br&gt;
Website: &lt;a href="https://codenoun.com" rel="noopener noreferrer"&gt;CodeNoun&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Integrate Algolia with Node.js for Full-Text Search</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Sat, 31 Aug 2024 19:03:53 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-integrate-algolia-with-nodejs-for-full-text-search-a8g</link>
      <guid>https://forem.com/codenoun/how-to-integrate-algolia-with-nodejs-for-full-text-search-a8g</guid>
      <description>&lt;p&gt;Full-text search is a critical feature for many applications, allowing users to find relevant information within large datasets quickly. Algolia, a popular search-as-a-service platform, offers a robust solution for implementing fast and accurate full-text search in Node.js applications.&lt;/p&gt;

&lt;p&gt;This article will guide you through integrating Algolia into your Node.js project, from initial setup to advanced search functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Algolia?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.algolia.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;Algolia&lt;/strong&gt;&lt;/a&gt; is a hosted search engine that provides developers with APIs to create fast and relevant search experiences. It offers features like typo tolerance, faceting, and custom ranking, making it an excellent choice for applications requiring sophisticated search capabilities.&lt;/p&gt;

&lt;p&gt;Algolia offers several benefits, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast search results (typically under 50ms)&lt;/li&gt;
&lt;li&gt;Easy integration with various platforms and frameworks&lt;/li&gt;
&lt;li&gt;Customizable ranking and relevance&lt;/li&gt;
&lt;li&gt;Scalability to handle large datasets and high query volumes&lt;/li&gt;
&lt;li&gt;Support for multiple languages and character sets&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up Your Node.js Environment
&lt;/h2&gt;

&lt;p&gt;Before integrating Algolia, ensure you have Node.js installed on your system. Create a new directory for your project and initialize it with npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;algolia-search-demo
&lt;span class="nb"&gt;cd &lt;/span&gt;algolia-search-demo
npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, install the Algolia JavaScript client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;algoliasearch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting Up Your Algolia Account and Application
&lt;/h2&gt;

&lt;p&gt;To use Algolia's services, you'll need to create an account and set up an application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sign up for a free Algolia account at &lt;a href="https://www.algolia.com/users/sign_up" rel="noopener noreferrer"&gt;https://www.algolia.com/users/sign_up&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;After logging in, create a new application&lt;/li&gt;
&lt;li&gt;Navigate to the API Keys section and note your &lt;strong&gt;Application ID&lt;/strong&gt; and &lt;strong&gt;Admin API Key&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Connecting to Algolia in Node.js&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With your Algolia credentials, you can now connect to the service from your Node.js application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;algoliasearch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;algoliasearch&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;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;algoliasearch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;YOUR_APPLICATION_ID&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="s1"&gt;YOUR_ADMIN_API_KEY&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;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;your_index_name&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;Replace &lt;code&gt;YOUR_APPLICATION_ID&lt;/code&gt; and &lt;code&gt;YOUR_ADMIN_API_KEY&lt;/code&gt; with your credentials and &lt;code&gt;your_index_name&lt;/code&gt; with a name for your search index.&lt;/p&gt;

&lt;p&gt;Full Tutorial: &lt;a href="https://codenoun.com/integrate-algolia-with-nodejs/" rel="noopener noreferrer"&gt;How to Integrate Algolia with Node.js for Full-Text Search&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Website: &lt;a href="https://codenoun.com" rel="noopener noreferrer"&gt;CodeNoun&lt;/a&gt;&lt;br&gt;
Telegram: &lt;a href="https://t.me/codenoun" rel="noopener noreferrer"&gt;CodeNoun&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Setting Up a Laravel for Local Development Environment</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Thu, 15 Aug 2024 04:48:09 +0000</pubDate>
      <link>https://forem.com/codenoun/setting-up-a-laravel-for-local-development-environment-2m8i</link>
      <guid>https://forem.com/codenoun/setting-up-a-laravel-for-local-development-environment-2m8i</guid>
      <description>&lt;p&gt;Laravel is a powerful PHP framework that simplifies web application development. To get started with Laravel, you need to set up a &lt;a href="https://codenoun.com/set-up-local-development-environment-laravel/" rel="noopener noreferrer"&gt;local development environment for Laravel&lt;/a&gt; that matches your production setup. Here's how you can do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Installing Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before diving into Laravel, ensure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PHP: Laravel requires PHP 7.4 or higher. You can install PHP via your package manager or download it directly from &lt;a href="https://www.php.net/" rel="noopener noreferrer"&gt;php.net&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Composer: This dependency manager is crucial for handling Laravel’s packages. Download and install it from &lt;a href="https://getcomposer.org" rel="noopener noreferrer"&gt;getcomposer.org&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Creating a New Laravel Project
&lt;/h2&gt;

&lt;p&gt;With PHP and Composer ready, you can create a new Laravel project using Composer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer create-project --prefer-dist laravel/laravel my-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;"my-project"&lt;/code&gt; with your desired project name. This command installs Laravel and its dependencies.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3. Setting Up a Local Server&lt;br&gt;
&lt;/code&gt;Laravel offers several ways to serve your project locally:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;PHP’s Built-in Server: This is the simplest method. Run &lt;code&gt;php artisan serve&lt;/code&gt; on your console. Your project will be accessible at &lt;a href="http://localhost:8000" rel="noopener noreferrer"&gt;http://localhost:8000&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Laravel Valet: A great option for Mac users, Valet provides a fast and minimalistic local environment. Follow the installation guide on &lt;a href="https://laravel.com/" rel="noopener noreferrer"&gt;Laravel's official site&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Homestead: Laravel Homestead is a pre-packaged Vagrant box that provides a development environment similar to your production setup. It’s ideal for those who need a complete and isolated environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker: For a containerized approach, Docker allows you to run your Laravel project in an isolated environment. You can use official Docker images or create a custom Dockerfile.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  4. Configuring Environment Variables
&lt;/h2&gt;

&lt;p&gt;Laravel uses a &lt;code&gt;.env&lt;/code&gt; file for configuration. Update this file with your local settings, such as database credentials, to match your local setup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Setting Up the Database
&lt;/h2&gt;

&lt;p&gt;Ensure that your database server is running and accessible. Update your .env file with the correct database credentials. If you're using MySQL, you can create a new database for your Laravel project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE my_project;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Connect to the database by updating the .env file with the database name, user, and password.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Running Migrations
&lt;/h2&gt;

&lt;p&gt;Laravel uses migrations to manage your database schema. Run the migrations to set up your tables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will create the necessary tables based on the migration files in your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With your local environment set up, you're ready to start developing your Laravel application. This setup will ensure your development process is smooth and closely mirrors your production environment.&lt;/p&gt;

&lt;p&gt;You can check another tutorial below:&lt;br&gt;
Blog: &lt;a href="https://codenoun.com" rel="noopener noreferrer"&gt;https://codenoun.com&lt;/a&gt;&lt;br&gt;
Telegram: &lt;a href="https://t.me/s/codenoun" rel="noopener noreferrer"&gt;https://t.me/s/codenoun&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Implement Kafka and Node.js in Microservice Architecture</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Fri, 09 Aug 2024 17:04:25 +0000</pubDate>
      <link>https://forem.com/codenoun/implement-kafka-and-nodejs-in-microservice-architecture-5h0h</link>
      <guid>https://forem.com/codenoun/implement-kafka-and-nodejs-in-microservice-architecture-5h0h</guid>
      <description>&lt;p&gt;When designing &lt;strong&gt;microservices architecture&lt;/strong&gt; for event-driven applications, integrating &lt;strong&gt;Apache Kafka and Node.js&lt;/strong&gt; can significantly enhance real-time data processing capabilities. In this article, we'll explore how to leverage &lt;strong&gt;Kafka Node.js integration&lt;/strong&gt; to build robust and scalable microservices that handle streaming data efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Apache Kafka in a Microservices Architecture?
&lt;/h2&gt;

&lt;p&gt;In a &lt;strong&gt;microservices architecture&lt;/strong&gt;, services need to communicate with each other efficiently. &lt;strong&gt;Apache Kafka&lt;/strong&gt; serves as a distributed event streaming platform that enables real-time data exchange between microservices. It decouples the services, allowing them to operate independently while processing large volumes of data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of Kafka in Event-Driven Applications
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Kafka's distributed architecture supports horizontal scaling, making it ideal for &lt;strong&gt;real-time data processing&lt;/strong&gt; in event-driven applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fault Tolerance&lt;/strong&gt;: Kafka ensures that data is reliably delivered, even in the event of failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;High Throughput&lt;/strong&gt;: Kafka can handle millions of events per second, providing high throughput for demanding microservices applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up Kafka Node.js Integration
&lt;/h2&gt;

&lt;p&gt;To &lt;a href="https://codenoun.com/apache-kafka-nodejs-microservices-architecture/" rel="noopener noreferrer"&gt;integrate &lt;strong&gt;Apache Kafka and Node.js&lt;/strong&gt; in a microservices&lt;/a&gt; environment, you'll need to set up Kafka as a message broker and connect it with your Node.js services. Here's a step-by-step guide:&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Kafka and Node.js
&lt;/h3&gt;

&lt;p&gt;First, ensure that &lt;strong&gt;&lt;a href="https://kafka.apache.org/" rel="noopener noreferrer"&gt;Apache Kafka&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt;&lt;/strong&gt; are installed on your system. You can install Kafka &amp;amp; Node.js by following the following articles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://codenoun.com/introduction-to-nodejs/" rel="noopener noreferrer"&gt;Introduction to Node.js&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://codenoun.com/getting-started-with-apache-kafka/" rel="noopener noreferrer"&gt;Getting Started With Apache Kafka&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://codenoun.com/how-to-integrate-kafka-nodejs/" rel="noopener noreferrer"&gt;How to Integrate Apache Kafka with Node.js&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install Kafka Node.js Client Library
&lt;/h3&gt;

&lt;p&gt;To connect &lt;strong&gt;Node.js&lt;/strong&gt; with &lt;strong&gt;Kafka&lt;/strong&gt;, you can use the &lt;code&gt;kafkajs&lt;/code&gt; library, a popular Kafka client for Node.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;kafkajs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Kafka Producer in Node.js
&lt;/h3&gt;

&lt;p&gt;In a &lt;strong&gt;microservices architecture&lt;/strong&gt;, a Kafka producer is responsible for sending messages to a Kafka topic. Below is a simple example of how to create a Kafka producer in Node.js:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kafkajs&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;kafka&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Kafka&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-producer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brokers&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="s1"&gt;localhost:9092&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;producer&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;sendMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Kafka&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="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a Kafka Consumer in Node.js
&lt;/h3&gt;

&lt;p&gt;A Kafka consumer is used to read messages from a Kafka topic. Here’s how you can create a consumer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kafkajs&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;kafka&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Kafka&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-consumer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brokers&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="s1"&gt;localhost:9092&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;groupId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-group&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;runConsumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-topic&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fromBeginning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;eachMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nf"&gt;runConsumer&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Case Study
&lt;/h2&gt;

&lt;p&gt;To illustrate the integration of Kafka and Node.js in a microservice architecture, consider the following case study:&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario
&lt;/h3&gt;

&lt;p&gt;We have two microservices:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Order Service:&lt;/strong&gt; Handles customer orders.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Product Service:&lt;/strong&gt; Manages product stocks.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whenever a purchase or transaction occurs in the &lt;strong&gt;Order Service&lt;/strong&gt;, it will to update the stock in the &lt;strong&gt;Product Service&lt;/strong&gt;. Kafka facilitates this communication by acting as a message broker.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Order Service:&lt;/strong&gt; Publishes order events to the &lt;code&gt;product-updates&lt;/code&gt; topic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inventory Service:&lt;/strong&gt; Consumes messages from the &lt;code&gt;product-updates&lt;/code&gt; topic and updates the inventory accordingly.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Order Service Producer Script
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Order Service&lt;/strong&gt; is responsible for handling purchase orders and sending messages to the &lt;strong&gt;Product Service&lt;/strong&gt; to update the stock. Here's how you can implement the &lt;strong&gt;Order Service&lt;/strong&gt; as a Kafka producer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// orderService.js&lt;/span&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="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kafkajs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Kafka producer configuration&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Kafka&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;order-service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brokers&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="s1"&gt;localhost:9092&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize Express app&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="nf"&gt;express&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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;placeOrder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quantity&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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&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;orderEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;eventType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ORDER_PLACED&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;product-updates&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;disconnect&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Order placed: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; for product: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// API endpoint to place an order&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/order&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quantity&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&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;body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Missing orderId, productId, or quantity&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;placeOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quantity&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="nf"&gt;status&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="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Order &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; placed successfully.`&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&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="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error placing order:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&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="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to place order&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="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Start the server&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3000&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="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Order Service API running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;



&lt;h3&gt;
  
  
  Product Service Consumer Script
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Product Service&lt;/strong&gt; consumes messages from the &lt;code&gt;product-updates&lt;/code&gt; Kafka topic and updates the product stock accordingly. Here's the implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// productService.js&lt;/span&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="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Kafka&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;kafkajs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Kafka consumer configuration&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Kafka&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;product-service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;brokers&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="s1"&gt;localhost:9092&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;groupId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;product-group&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize Express app&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="nf"&gt;express&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="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&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;updateStock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;product-updates&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;fromBeginning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;eachMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;orderEvent&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="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Received order: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;orderId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Product: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Quantity: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// Simulate stock update&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Updating stock for product: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;orderEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;productId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="c1"&gt;// logic to update stock&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Start the Product Service to listen for messages&lt;/span&gt;
&lt;span class="nf"&gt;updateStock&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&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;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Start the server&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;3001&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="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;PORT&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Product Service API running on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;



&lt;p&gt;Start the &lt;strong&gt;Product Service&lt;/strong&gt; first, as it needs to listen for incoming messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="nx"&gt;productService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Product Service&lt;/strong&gt; will start listening on port &lt;code&gt;3001&lt;/code&gt; (or another port if specified).&lt;/p&gt;

&lt;p&gt;Start the &lt;strong&gt;Order Service&lt;/strong&gt; with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;node&lt;/span&gt; &lt;span class="nx"&gt;orderService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;js&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;Order Service&lt;/strong&gt; will be available on port &lt;code&gt;3000&lt;/code&gt; (or another port if specified).&lt;/p&gt;

&lt;p&gt;You can place an order by sending a POST request to the &lt;strong&gt;Order Service&lt;/strong&gt; API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-X&lt;/span&gt; POST http://localhost:3000/order &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-H&lt;/span&gt; &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
&lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="s1"&gt;'{
  "orderId": "order-789",
  "productId": "product-123",
  "quantity": 5
}'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When an order is placed, the &lt;strong&gt;Order Service&lt;/strong&gt; will send a Kafka message, and the &lt;strong&gt;Product Service&lt;/strong&gt; will consume that message to update the stock:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Received order: order-789, Product: product-123, Quantity: 5
Updating stock &lt;span class="k"&gt;for &lt;/span&gt;product: product-123
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Integrating &lt;strong&gt;Apache Kafka and Node.js&lt;/strong&gt; in your &lt;strong&gt;microservices architecture&lt;/strong&gt; allows you to build highly scalable and resilient &lt;strong&gt;event-driven applications&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By following best practices and leveraging Kafka’s powerful features, you can efficiently process &lt;strong&gt;real-time data&lt;/strong&gt; and create a robust communication layer between your microservices.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>eventdriven</category>
      <category>webdev</category>
      <category>microservices</category>
    </item>
    <item>
      <title>How to Create a Cloudflare Worker in JavaScript</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Mon, 05 Aug 2024 01:13:28 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-create-a-cloudflare-worker-in-javascript-32k2</link>
      <guid>https://forem.com/codenoun/how-to-create-a-cloudflare-worker-in-javascript-32k2</guid>
      <description>&lt;p&gt;Cloudflare Workers have revolutionized the way we deploy and run JavaScript code at the edge. This guide will walk you through the process of creating a Cloudflare Worker using JavaScript, from setup to deployment.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Cloudflare Workers?
&lt;/h2&gt;

&lt;p&gt;Cloudflare Workers is a game-changing technology that allows developers to run JavaScript code at the edge of Cloudflare's global network. This means your code executes closer to your users, resulting in lightning-fast response times and improved scalability.&lt;br&gt;
Getting Started&lt;br&gt;
Before diving in, make sure you have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A basic understanding of JavaScript (ES6 or later)&lt;/li&gt;
&lt;li&gt;A Cloudflare account (free tier available)&lt;/li&gt;
&lt;li&gt;Node.js and npm installed on your machine&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setting Up Your Environment
&lt;/h2&gt;

&lt;p&gt;Install Wrangler CLI, Cloudflare's official command-line tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -g wrangler

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

&lt;/div&gt;



&lt;p&gt;Authenticate Wrangler with your Cloudflare account:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wrangler login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating Your First Worker
&lt;/h2&gt;

&lt;p&gt;Initialize a new project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wrangler init my-worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the generated index.js file and add your Worker code. Here's a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;addEventListener('fetch', event =&amp;gt; {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello, World!', {
    headers: { 'content-type': 'text/plain' },
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy your Worker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wrangler publish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep your code simple and focused&lt;/li&gt;
&lt;li&gt;Leverage Cloudflare's built-in caching&lt;/li&gt;
&lt;li&gt;Prioritize security by validating inputs&lt;/li&gt;
&lt;li&gt;Monitor and optimize performance using Cloudflare's analytics&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cloudflare Workers offers a powerful way to enhance your web applications. By following this guide, you'll be well on your way to creating efficient, scalable, and high-performance solutions using JavaScript at the edge.&lt;/p&gt;

&lt;p&gt;For an in-depth exploration of these concepts and more advanced techniques, check out the full tutorial about &lt;a href="https://codenoun.com/how-to-create-cloudflare-worker-in-javascript/" rel="noopener noreferrer"&gt;How to Create a Cloudflare Worker in Javascript&lt;/a&gt;. This comprehensive resource provides additional details, code samples, and best practices to help you make the most of Cloudflare Workers in your projects.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to connect PostgreSQL with Node.js and Sequelize</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Thu, 01 Aug 2024 17:59:49 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-connect-postgresql-with-nodejs-and-sequelize-26jb</link>
      <guid>https://forem.com/codenoun/how-to-connect-postgresql-with-nodejs-and-sequelize-26jb</guid>
      <description>&lt;p&gt;When it comes to working with databases, developers often face the choice between using raw database queries or leveraging libraries that abstract away some of the complexity. Sequelize is one such library—a popular Node.js ORM (Object-Relational Mapper) that works with PostgreSQL, MySQL, and other relational databases. In this tutorial, we will dive into how to use Sequelize on Node.js to connect with a PostgreSQL database, covering installation, configuration, model creation, and performing CRUD operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install and Configure Sequelize
&lt;/h3&gt;

&lt;p&gt;Sequelize simplifies database interactions by providing an abstraction layer over SQL queries. It utilizes the &lt;code&gt;pg&lt;/code&gt; library under the hood to connect to PostgreSQL. To get started with Sequelize, follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialize Your Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you’re starting with a new project, initialize a Node.js project to create a &lt;code&gt;package.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Install Sequelize and PostgreSQL Driver&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install Sequelize along with the PostgreSQL driver &lt;code&gt;pg&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install pg sequelize
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Set Up Database Configuration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In your Node.js application, you first need to define your PostgreSQL database access variables:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;postgres_user&amp;gt;&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;host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&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;database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;postgres_db_name&amp;gt;&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;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;postgres_password&amp;gt;&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;postgres_port&amp;gt;&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;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Import Sequelize&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Import the necessary objects from Sequelize:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Sequelize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;DataTypes&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sequelize&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;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initialize Sequelize&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a new Sequelize instance with your database configuration:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sequelize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Sequelize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;dialect&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;postgres&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Here, we specify &lt;code&gt;dialect: 'postgres'&lt;/code&gt; to indicate we are using PostgreSQL. We also disable SQL query logging to keep the console output clean, though you might enable it during debugging.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating a Sequelize Model
&lt;/h3&gt;

&lt;p&gt;Models in Sequelize represent tables in your database. Each model defines the structure of the corresponding table and provides methods for interacting with it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define a Model&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Suppose you have a table named &lt;code&gt;cats&lt;/code&gt; with columns &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;. You can define a Sequelize model for this table as follows:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Cat&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DataTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;STRING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;allowNull&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;DataTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;INTEGER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;allowNull&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
  &lt;span class="p"&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;sequelize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;modelName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;In this example:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- `Cat` extends Sequelize’s `Model` class.
- `init()` sets up the model with column definitions and configuration.
- We use `DataTypes.STRING` and `DataTypes.INTEGER` to define column types.
- `timestamps: false` disables automatic timestamp fields (`createdAt` and `updatedAt`).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Querying Data
&lt;/h3&gt;

&lt;p&gt;Sequelize provides a range of methods to query your database. Here’s how to retrieve data using Sequelize:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Retrieve All Records&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To fetch all records from the &lt;code&gt;cats&lt;/code&gt; table:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;&lt;code&gt;findAll()&lt;/code&gt; returns a promise that resolves to an array of instances representing the rows in the table.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Limit the Columns Retrieved&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you only need certain columns, use the &lt;code&gt;attributes&lt;/code&gt; option:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;attributes&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="s1"&gt;age&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add a WHERE Clause&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To filter results, use the &lt;code&gt;where&lt;/code&gt; option. For example, to find all cats aged 8:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
  &lt;span class="p"&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;To find all cats aged 5 or older:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Op&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sequelize&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;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&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;Op&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;gte&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="p"&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Apply Sorting and Limiting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To sort results and limit the number of rows returned:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;order&lt;/span&gt;&lt;span class="p"&gt;:&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="s1"&gt;name&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="s1"&gt;DESC&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="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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Inserting Data
&lt;/h3&gt;

&lt;p&gt;To insert a new record into the database, use the &lt;code&gt;create()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Garfield&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;New Cat:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Updating Data
&lt;/h3&gt;

&lt;p&gt;To update existing records, use the &lt;code&gt;update()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Garfield&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="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to update all rows (be careful with this operation), omit the &lt;code&gt;where&lt;/code&gt; clause:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;Cat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&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;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this tutorial, we explored &lt;a href="https://codenoun.com/connect-postgresql-nodejs-sequelize/" rel="noopener noreferrer"&gt;how to use Sequelize to interact with a PostgreSQL&lt;/a&gt; database. We started by installing and configuring Sequelize, then created models to represent database tables.&lt;/p&gt;

&lt;p&gt;We learned how to query, insert, and update data using Sequelize’s methods. This powerful ORM simplifies working with relational databases by providing a higher-level abstraction over SQL queries.&lt;/p&gt;

&lt;p&gt;For further exploration of Sequelize and its features, consider the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sequelize Documentation&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.postgresql.org/docs/" rel="noopener noreferrer"&gt;PostgreSQL Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Node.js Documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By leveraging Sequelize, you can streamline your database interactions and focus more on building robust applications. Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>postgres</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting Started With Apache Kafka</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Thu, 01 Aug 2024 17:57:05 +0000</pubDate>
      <link>https://forem.com/codenoun/getting-started-with-apache-kafka-d5h</link>
      <guid>https://forem.com/codenoun/getting-started-with-apache-kafka-d5h</guid>
      <description>&lt;p&gt;Apache Kafka is a powerful, distributed event streaming platform capable of handling trillions of events a day. Originally developed by LinkedIn and open-sourced in early 2011, Kafka has evolved into a central backbone for many modern data architectures. In this guide, we will walk you through everything you need to get started with Apache Kafka, from understanding its architecture to setting it up and performing basic operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction to Apache Kafka
&lt;/h3&gt;

&lt;p&gt;Apache Kafka is designed to handle real-time data feeds. It works as a high-throughput, low-latency platform for handling data streams. Kafka is often used for building real-time streaming data pipelines and applications that adapt to the data stream. Some common use cases include log aggregation, real-time analytics, and stream processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Concepts and Terminology
&lt;/h3&gt;

&lt;p&gt;Before diving into the setup and operations, it's essential to understand some key concepts and terminology in Kafka:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Producer&lt;/strong&gt;: An application that sends messages to a Kafka topic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consumer&lt;/strong&gt;: An application that reads messages from a Kafka topic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Topic&lt;/strong&gt;: A category or feed name to which messages are sent by producers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Broker&lt;/strong&gt;: A Kafka server that stores and serves Kafka topics.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition&lt;/strong&gt;: A division of a topic for scalability and parallel processing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Offset&lt;/strong&gt;: A unique identifier for each message within a partition.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up Apache Kafka
&lt;/h3&gt;

&lt;p&gt;Setting up Apache Kafka involves several steps, including downloading the necessary software, configuring it, and starting the services. In this section, we'll provide a detailed walkthrough to ensure you can get your Kafka environment up and running smoothly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Before you start setting up Kafka, make sure your system meets the following prerequisites:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Java Development Kit (JDK)&lt;/strong&gt;: Kafka requires Java 8 or later. You can check your Java version with the following command:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;java &lt;span class="nt"&gt;-version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;If Java is not installed, you can download and install it from the &lt;a href="https://www.oracle.com/java/technologies/javase-downloads.html" rel="noopener noreferrer"&gt;Oracle website&lt;/a&gt; or use a package manager like &lt;code&gt;apt&lt;/code&gt; for Debian-based systems or &lt;code&gt;brew&lt;/code&gt; for macOS:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# For Debian-based systems&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;openjdk-11-jdk

&lt;span class="c"&gt;# For macOS&lt;/span&gt;
brew &lt;span class="nb"&gt;install &lt;/span&gt;openjdk@11
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Apache ZooKeeper&lt;/strong&gt;: Kafka uses ZooKeeper to manage distributed configurations and synchronization. ZooKeeper is bundled with Kafka, so you don't need to install it separately.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Download and Install Kafka
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Download Kafka&lt;/strong&gt;: Visit the &lt;a href="https://kafka.apache.org/downloads" rel="noopener noreferrer"&gt;official Apache Kafka download page&lt;/a&gt; and download the latest version of Kafka. As of writing, Kafka 2.8.0 is the latest stable release.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://downloads.apache.org/kafka/2.8.0/kafka_2.13-2.8.0.tgz
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Extract the Downloaded File&lt;/strong&gt;: Extract the tar file to a directory of your choice.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-xzf&lt;/span&gt; kafka_2.13-2.8.0.tgz
&lt;span class="nb"&gt;cd &lt;/span&gt;kafka_2.13-2.8.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start ZooKeeper&lt;/strong&gt;: Kafka requires ZooKeeper to run. Start the ZooKeeper service using the provided configuration file.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/zookeeper-server-start.sh config/zookeeper.properties
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;ZooKeeper should start on the default port 2181. You should see log messages indicating that ZooKeeper is up and running.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Start Kafka Broker&lt;/strong&gt;: Open a new terminal window and start the Kafka broker using the provided configuration file.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/kafka-server-start.sh config/server.properties
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Kafka should start on the default port 9092. You should see log messages indicating that the Kafka broker is up and running.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Kafka Configuration
&lt;/h3&gt;

&lt;p&gt;While the default configurations are suitable for development and testing, you may need to customize the settings for a production environment. Some key configuration files include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;server.properties&lt;/strong&gt;: This file contains configurations for the Kafka broker, such as broker ID, log directory, and listeners.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;zookeeper.properties&lt;/strong&gt;: This file contains configurations for ZooKeeper, such as data directory and client port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can edit these configuration files to suit your needs. For example, to change the log directory, you can edit the &lt;code&gt;log.dirs&lt;/code&gt; property in the &lt;code&gt;server.properties&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log.dirs=/path/to/your/kafka-logs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating Systemd Service Files
&lt;/h3&gt;

&lt;p&gt;For ease of management, especially on Linux servers, you can create systemd service files for ZooKeeper and Kafka. This allows you to start, stop, and restart these services using systemctl.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ZooKeeper Service File&lt;/strong&gt;: Create a file named &lt;code&gt;zookeeper.service&lt;/code&gt; in the &lt;code&gt;/etc/systemd/system/&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Apache ZooKeeper
After=network.target

[Service]
Type=simple
ExecStart=/path/to/kafka/bin/zookeeper-server-start.sh /path/to/kafka/config/zookeeper.properties
ExecStop=/path/to/kafka/bin/zookeeper-server-stop.sh
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Kafka Service File&lt;/strong&gt;: Create a file named &lt;code&gt;kafka.service&lt;/code&gt; in the &lt;code&gt;/etc/systemd/system/&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Unit]
Description=Apache Kafka
After=zookeeper.service

[Service]
Type=simple
ExecStart=/path/to/kafka/bin/kafka-server-start.sh /path/to/kafka/config/server.properties
ExecStop=/path/to/kafka/bin/kafka-server-stop.sh
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Enable and Start Services&lt;/strong&gt;: Enable and start the services using &lt;code&gt;systemctl&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;zookeeper
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start zookeeper

&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;kafka
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start kafka
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You can now manage ZooKeeper and Kafka using standard systemctl commands (&lt;code&gt;start&lt;/code&gt;, &lt;code&gt;stop&lt;/code&gt;, &lt;code&gt;status&lt;/code&gt;, &lt;code&gt;restart&lt;/code&gt;).&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Verifying the Installation
&lt;/h3&gt;

&lt;p&gt;To verify that your Kafka setup is working correctly, you can perform some basic operations such as creating a topic, producing messages, and consuming messages.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Creating a Topic&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/kafka-topics.sh &lt;span class="nt"&gt;--create&lt;/span&gt; &lt;span class="nt"&gt;--topic&lt;/span&gt; test-topic &lt;span class="nt"&gt;--bootstrap-server&lt;/span&gt; localhost:9092 &lt;span class="nt"&gt;--partitions&lt;/span&gt; 1 &lt;span class="nt"&gt;--replication-factor&lt;/span&gt; 1
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You should see a confirmation message indicating that the topic has been created successfully.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Producing Messages&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/kafka-console-producer.sh &lt;span class="nt"&gt;--topic&lt;/span&gt; test-topic &lt;span class="nt"&gt;--bootstrap-server&lt;/span&gt; localhost:9092
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Type a few messages in the console and press Enter after each message.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Consuming Messages&lt;/strong&gt;:&lt;br&gt;
Open a new terminal window and run:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/kafka-console-consumer.sh &lt;span class="nt"&gt;--topic&lt;/span&gt; test-topic &lt;span class="nt"&gt;--from-beginning&lt;/span&gt; &lt;span class="nt"&gt;--bootstrap-server&lt;/span&gt; localhost:9092
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;You should see the messages you produced in the previous step.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these steps, you should have a fully functional Apache Kafka environment set up on your system. This setup forms the foundation for developing and deploying real-time data streaming applications using Kafka.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Getting started with Apache Kafka can seem daunting, but with the right guidance, you can quickly get up to speed. This guide provided a comprehensive introduction to Kafka, from installation to basic operations and building simple producers and consumers. As you continue to explore Kafka, you will uncover its full potential for building robust, real-time data pipelines.&lt;/p&gt;

&lt;p&gt;By following this guide, you’ve taken the first steps in &lt;a href="https://codenoun.com/getting-started-with-apache-kafka/" rel="noopener noreferrer"&gt;mastering Apache Kafka&lt;/a&gt;. Happy streaming!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>kafka</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction to Node.js</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Thu, 01 Aug 2024 17:52:02 +0000</pubDate>
      <link>https://forem.com/codenoun/how-to-install-postgresql-in-ubuntu-2204-3fgj</link>
      <guid>https://forem.com/codenoun/how-to-install-postgresql-in-ubuntu-2204-3fgj</guid>
      <description>&lt;p&gt;Node.js is a powerful, open-source runtime environment that allows developers to run JavaScript on the server side. Its non-blocking, event-driven architecture ensures high performance and scalability, making it essential for modern web development. Node.js excels in handling real-time applications, such as chat systems and collaborative tools, with minimal overhead.&lt;/p&gt;

&lt;p&gt;This article &lt;a href="https://codenoun.com/introduction-to-nodejs/" rel="noopener noreferrer"&gt;introduces Node.js&lt;/a&gt;, highlighting its core features and advantages. Whether you're new to backend development or looking to enhance your skills, this guide will help you understand why Node.js is a cornerstone of contemporary web development.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Node.js?
&lt;/h2&gt;

&lt;p&gt;Node.js is an open-source, cross-platform runtime environment that executes JavaScript code outside of a browser, primarily on the server side. It uses the V8 JavaScript engine, known for its speed and efficiency. Core features of Node.js include its event-driven, non-blocking I/O model, which ensures high performance and scalability.&lt;/p&gt;

&lt;p&gt;Node.js was created by Ryan Dahl in 2009, revolutionizing server-side programming by enabling JavaScript to handle backend tasks. Since its inception, it has evolved significantly, gaining widespread adoption and a vibrant community.&lt;/p&gt;

&lt;p&gt;Compared to other server-side technologies, Node.js offers faster execution, better handling of concurrent connections, and a unified language for both client and server development.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Your Environment
&lt;/h2&gt;

&lt;p&gt;To begin with Node.js development, you'll first need to install Node.js and npm (Node Package Manager). Head over to the &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;official Node.js website&lt;/a&gt; and download the installer suitable for your operating system—be it Windows, macOS, or Linux.&lt;/p&gt;

&lt;p&gt;The installer includes npm, a crucial tool for managing packages. Once the download is complete, run the installer and follow the on-screen instructions to complete the installation process. After installation, you can verify that Node.js and npm are correctly installed by opening your terminal or command prompt and typing &lt;code&gt;node -v&lt;/code&gt; and &lt;code&gt;npm -v&lt;/code&gt;. You should see the version numbers of Node.js and npm, confirming that the installation was successful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-Step Instructions:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;official Node.js website&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Download the installer for your operating system (Windows, macOS, or Linux).&lt;/li&gt;
&lt;li&gt;Run the downloaded installer and follow the on-screen instructions to complete the installation.&lt;/li&gt;
&lt;li&gt;Open your terminal or command prompt.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Verify the installation by typing the following commands:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;-v&lt;/span&gt;
npm &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should see the version numbers for Node.js and npm, confirming the successful installation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Building Your First Application
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Create a Project Directory:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your terminal or command prompt.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the location where you want to create your project and create a new directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;my-nodejs-project
&lt;span class="nb"&gt;cd &lt;/span&gt;my-nodejs-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Initialize a Node.js Project:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In your project directory, run:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Follow the prompts to set up your &lt;code&gt;package.json&lt;/code&gt; file. You can press &lt;code&gt;Enter&lt;/code&gt; to accept the default settings.&lt;br&gt;
&lt;strong&gt;3. Create a Simple Node.js Server:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a new file named &lt;code&gt;app.js&lt;/code&gt; in your project directory:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&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;hostname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;127.0.0.1&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&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;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&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;statusCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;200&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="nf"&gt;setHeader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Content-Type&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="s1"&gt;text/plain&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, World!&lt;/span&gt;&lt;span class="se"&gt;\n&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;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hostname&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server running at http://&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;hostname&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Run Your Node.js Server:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;In your terminal, run:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;node app.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should see the message &lt;code&gt;Server running at http://127.0.0.1:3000/&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open your web browser and navigate to &lt;code&gt;http://127.0.0.1:3000/&lt;/code&gt; to see your "Hello, World!" message.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulations! You've set up your Node.js environment, created your first Node.js project, and run a simple server.From here, you can start exploring more advanced features and build more complex applications.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Cara Membuat Bilangan Fibonacci Dengan PHP</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Tue, 27 Sep 2022 08:47:21 +0000</pubDate>
      <link>https://forem.com/codenoun/cara-membuat-bilangan-fibonacci-dengan-php-3g89</link>
      <guid>https://forem.com/codenoun/cara-membuat-bilangan-fibonacci-dengan-php-3g89</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;a href="https://ruangkoding.id/cara-membuat-deret-fibonacci-dengan-php/" rel="noopener noreferrer"&gt;Cara Membuat Bilangan Fibonacci Dengan PHP&lt;/a&gt; -&lt;/strong&gt; Fibonacci adalah sebuah deret angka dimana dua angka sebelumnya ditambahkan untuk mendapatkan angka berikutnya, dimulai dengan 0 dan 1. Pada artikel ini, kita akan belajar tentang cara membuat deret Fibonacci di PHP dengan dua cara yaitu dengan menggunakan cara iteratif dan rekursif.&lt;/p&gt;

&lt;p&gt;Pada contoh kasus, kita memiliki angka n, kita perlu mencari bilangan Fibonacci hingga suku ke-n.&lt;/p&gt;

&lt;p&gt;Contoh :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input : 10
Output : 0 1  1 2 3 5 8 13 21 34

Input : 15
Output : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Membuat deret Fibonacci dengan cara rekursif
&lt;/h2&gt;

&lt;p&gt;Cara rekursif adalah cara di mana kita berulang kali memanggil fungsi yang sama sampai kondisi dasar cocok untuk mengakhiri rekursi.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php  
function Fibonacci($number){
    if ($number == 0)
        return 0;    
    else if ($number == 1)
        return 1;    
    else
        return (Fibonacci($number-1) + 
                Fibonacci($number-2));
}

$number = 10;
for ($counter = 0; $counter &amp;lt; $number; $counter++){  
    echo Fibonacci($counter),' ';
}
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kode diatas akan menghasilkan output seperti berikut&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1  1 2 3 5 8 13 21 34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Membuat deret Fibonacci dengan cara iteratif
&lt;/h2&gt;

&lt;p&gt;Pada cara ini, kita menginisialisasi angka pertama dan kedua menjadi 0 dan 1. Setelah itu, kita mencetak angka pertama dan kedua. Kemudian kita mengirimkan aliran ke loop while secara berulang di mana kita mendapatkan nomor berikutnya dengan menambahkan dua nomor sebelumnya dan secara bersamaan kita menukar nomor pertama dengan yang kedua dan yang kedua dengan yang ketiga.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
function Fibonacci($n){
    $num1 = 0;
    $num2 = 1;

    $counter = 0;
    while ($counter &amp;lt; $n){
        echo ' '.$num1;
        $num3 = $num2 + $num1;
        $num1 = $num2;
        $num2 = $num3;
        $counter = $counter + 1;
    }
}
$n = 10;
Fibonacci($n);
?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kode diatas akan menghasilkan output seperti berikut :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0 1  1 2 3 5 8 13 21 34
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Itulah cara membuat bilangan Fibonacci dengan menggunakan cara rekursif dan iteratif. Jangan lupa kunjungi &lt;a href="https://ruangkoding.id" rel="noopener noreferrer"&gt;Ruang Koding&lt;/a&gt; untuk update terbaru lainnya. Selamat mencoba &amp;amp; semoga bermanfaat!&lt;/p&gt;

</description>
      <category>php</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Begini Cara Mengatasi Suara Discord Patah-patah di Laptop Dengan Mudah</title>
      <dc:creator>Riky Fahri Hasibuan</dc:creator>
      <pubDate>Tue, 21 Dec 2021 06:39:00 +0000</pubDate>
      <link>https://forem.com/codenoun/begini-cara-mengatasi-suara-discord-patah-patah-di-laptop-dengan-mudah-lie</link>
      <guid>https://forem.com/codenoun/begini-cara-mengatasi-suara-discord-patah-patah-di-laptop-dengan-mudah-lie</guid>
      <description>&lt;p&gt;Suara discord kalian kerap putus-putus? begini cara mengatasi suara Discord patah-patah di Android dengan Mudah!&lt;/p&gt;

&lt;p&gt;Bagi kalian beberapa gamer tentu saja tahu sebuah program yang membantu kita untuk melakukan komunikasi dengan teman-teman kalian.&lt;/p&gt;

&lt;p&gt;Berkomunikasi dalam beberapa permainan itu pastinya sangat penting, bahkan juga beberapa game mengharuskan para pemainnya untuk menggunakan komunikasi via voice call.&lt;/p&gt;

&lt;p&gt;Contoh seperti game yang trending akhir-akhir ini yaitu diantaranya Among Us, game batte royale PUBG Mobile, dan ada banyak contoh game lainnya.&lt;/p&gt;

&lt;p&gt;Sejumlah game memang menyediakan feature voice yang memudahkan para pemainnya untuk tidak menggunakan aplikasi yang lainnya.&lt;/p&gt;

&lt;p&gt;Namun terkadang voice di dalam game tersebut tidak sebaik dan selancar yang kita kira. Salah satu aplikasi gaming voice yang populer yaitu &lt;a href="https://discord.com/" rel="noopener noreferrer"&gt;Discord&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Discord jadi wadah untuk para gamers untuk berkomunikasi satu sama lain, ini karena discord tidak membebankan game yang kita mainkan.&lt;/p&gt;

&lt;p&gt;Selama aplikasi ini berjalan, game yang kita mainkan tidak akan lag atau sebagainya, karena program ini tidak berat sama sekali.&lt;/p&gt;

&lt;p&gt;Terkadang untuk pemain mobile, kerap mengalami masalah di mana suara discord sering putus-putus.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cara Mengatasi Suara Discord Putus-putus
&lt;/h2&gt;

&lt;p&gt;Nah, di artikel ini &lt;a href="https://ruangkoding.id" rel="noopener noreferrer"&gt;Ruang Koding&lt;/a&gt; akan kasih tahu cara mengatasi suara Discord putus-putus di Laptop dan juga bagaimana cara agar suara Discord jernih.&lt;/p&gt;

&lt;p&gt;Cara agar Discord tidak lag adalah kalian dapat mengubah server di channel Discord kalian, dan mengubahnya ke server luar supaya jaringannya lebih stabil.&lt;/p&gt;

&lt;p&gt;Server yang selama ini dianggap bagus yaitu Sydney, India, dan Singapore yang dapat kalian jadikan server inti.&lt;/p&gt;

&lt;p&gt;Langkahnya sangat gampang, dan cuma dapat dilakukan oleh yang membuat server tersebut:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Buka server discord dan, lalu tekan tombol titik tiga di samping nama server kalian.&lt;/li&gt;
&lt;li&gt;Pilih Settings &amp;gt; Overview&lt;/li&gt;
&lt;li&gt;Dari sana kalian akan melihat ada Server Region, dan biasanya kita sudah masuk ke server Singapore.&lt;/li&gt;
&lt;li&gt;Apabila di server tersebut kalian masih putus-putus, maka ubahlah ke server lainnya yang sudah kita sarankan di atas.&lt;/li&gt;
&lt;li&gt;Pilih Server Region, lalu tentukan server yang kalian inginkan.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nah setelah itu kalian dijamin tidak akan mengalami &lt;a href="https://ruangkoding.id/cara-mengatasi-suara-discord-patah-patah/" rel="noopener noreferrer"&gt;suara Discord putus-putus&lt;/a&gt; dan dapat bermain bersama teman kalian dengan lancar.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>windows</category>
      <category>game</category>
    </item>
  </channel>
</rss>
