<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Arash Arora]]></title><description><![CDATA[Arash Arora is a Web Developer with knowledge of MERN STACK. Currently pursuing final year of BTech in Computer Science Engineering with Cyber Security as a spe]]></description><link>https://blogs.arasharora.com</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 05:04:02 GMT</lastBuildDate><atom:link href="https://blogs.arasharora.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🧰 A Beginner's Guide to NGINX and NGINX Proxy Manager]]></title><description><![CDATA[Whether you’re self-hosting applications or deploying websites in production, reverse proxies are one of the most powerful tools in your DevOps toolbox. At the heart of it all is NGINX—a versatile, open-source server that can function as a web server...]]></description><link>https://blogs.arasharora.com/a-beginners-guide-to-nginx-and-nginx-proxy-manager</link><guid isPermaLink="true">https://blogs.arasharora.com/a-beginners-guide-to-nginx-and-nginx-proxy-manager</guid><category><![CDATA[Homelab]]></category><category><![CDATA[nginx]]></category><category><![CDATA[Devops]]></category><category><![CDATA[server]]></category><category><![CDATA[Reverse Proxy]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[Arash Arora]]></dc:creator><pubDate>Mon, 16 Jun 2025 14:42:05 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1750085173417/2744042f-977d-4c6c-90f4-a9ae761e0292.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Whether you’re self-hosting applications or deploying websites in production, <strong>reverse proxies</strong> are one of the most powerful tools in your DevOps toolbox. At the heart of it all is <strong>NGINX</strong>—a versatile, open-source server that can function as a web server, reverse proxy, load balancer, and even a mail proxy.</p>
<p>However, writing manual NGINX configurations—especially for multiple services, subdomains, and SSL certificates—can quickly become tedious, redundant and error-prone.</p>
<p>That’s where <strong>NGINX Proxy Manager (NPM)</strong> steps in to simplify everything with a clean and user-friendly interface.</p>
<p>In this blog, we’ll begin with the basics of NGINX, and then walk you through how to deploy NGINX Proxy Manager in just a few steps.</p>
<hr />
<h2 id="heading-what-is-nginx">🧠 What is NGINX?</h2>
<p><strong>NGINX</strong> (pronounced <em>engine-x</em>) is a high-performance, open-source web server that’s designed for scalability and efficiency. It can be used for:</p>
<ul>
<li><p>Serving static files (HTML, CSS, JavaScript)</p>
</li>
<li><p>Acting as a <strong>reverse proxy</strong> (routing external traffic to internal services)</p>
</li>
<li><p>Load balancing between servers</p>
</li>
<li><p>Caching for faster delivery</p>
</li>
<li><p>SSL termination (handling HTTPS connections)</p>
</li>
</ul>
<p>Originally built to solve the <strong>C10K problem</strong>—handling 10,000+ concurrent connections—NGINX has become one of the most popular servers in the world.</p>
<h2 id="heading-what-is-a-reverse-proxy">🔄 What is a Reverse Proxy?</h2>
<p>A <strong>reverse proxy</strong> acts as a gateway: it receives client requests, forwards them to the appropriate backend service, and returns the response to the client. This allows you to:</p>
<ul>
<li><p>Host multiple apps on a single server (e.g., <a target="_blank" href="http://app1.example.com"><code>app1.example.com</code></a>, <a target="_blank" href="http://app2.example.com"><code>app2.example.com</code></a>)</p>
</li>
<li><p>Serve all apps over HTTPS using <strong>Let’s Encrypt</strong></p>
</li>
<li><p>Hide internal infrastructure from the public</p>
</li>
<li><p>Apply rate limiting, access control, or logging policies</p>
</li>
</ul>
<h2 id="heading-installing-nginx-the-traditional-way">⚙️ Installing NGINX the Traditional Way</h2>
<p>On most Linux distributions, you can install NGINX with:</p>
<pre><code class="lang-bash">sudo apt update -y
sudo apt install nginx -y
</code></pre>
<p>To verify NGINX is running:</p>
<pre><code class="lang-bash">systemctl status nginx
</code></pre>
<p>You should be able to visit <code>http://&lt;your-server-ip&gt;</code> and see the NGINX welcome page.</p>
<p>To configure new sites manually, you’ll edit files under <code>/etc/nginx/sites-available/</code> and link them to <code>/etc/nginx/sites-enabled/</code></p>
<h2 id="heading-the-problem-manual-nginx-config-is-a-pain">😵‍💫 The Problem: Manual NGINX Config is a Pain</h2>
<p>For each new service or domain, you have to:</p>
<ul>
<li><p>Add a new subdomain</p>
</li>
<li><p>Set up SSL with Let’s Encrypt</p>
</li>
<li><p>Configure proxying rules</p>
</li>
<li><p>Handle authentication and security settings</p>
</li>
<li><p>Test your NGINX config and reload the service</p>
</li>
</ul>
<p>This process is powerful but <strong>not beginner-friendly</strong> and quickly becomes a burden when managing many services.</p>
<h2 id="heading-the-solution-nginx-proxy-manager-npm">🎉 The Solution: NGINX Proxy Manager (NPM)</h2>
<p><strong>NGINX Proxy Manager</strong> is a Docker-based application that wraps the power of NGINX in an easy-to-use <strong>web interface</strong>.</p>
<h3 id="heading-key-features">🔑 Key Features</h3>
<ul>
<li><p>Intuitive web UI to manage proxy hosts</p>
</li>
<li><p>Automatic SSL certificates via Let’s Encrypt</p>
</li>
<li><p>Redirection and 404 host handling</p>
</li>
<li><p>Access control via IP or basic auth</p>
</li>
<li><p>Custom NGINX config snippets</p>
</li>
<li><p>Lightweight and easy to deploy via Docker</p>
</li>
</ul>
<h2 id="heading-how-to-install-nginx-proxy-manager-on-ubuntudebian">🧱 How to Install NGINX Proxy Manager on Ubuntu/Debian</h2>
<h3 id="heading-prerequisites">Prerequisites</h3>
<ul>
<li><p>A Linux server or cloud VPS</p>
</li>
<li><p>Docker and Docker Compose installed</p>
</li>
<li><p>A registered domain (optional but recommended)</p>
</li>
</ul>
<h3 id="heading-step-1-install-docker-amp-docker-compose">Step 1: Install Docker &amp; Docker Compose</h3>
<pre><code class="lang-bash">sudo apt update
sudo apt install docker.io docker-compose -y
</code></pre>
<p>Verify Docker is running:</p>
<pre><code class="lang-bash">sudo systemctl status docker
</code></pre>
<p>If inactive, enable and start it:</p>
<pre><code class="lang-bash">sudo systemctl <span class="hljs-built_in">enable</span> docker
sudo systemctl start docker
</code></pre>
<h3 id="heading-step-2-create-the-docker-compose-file">Step 2: Create the Docker Compose File</h3>
<ul>
<li><p>Create a working directory:</p>
<pre><code class="lang-bash">  mkdir npm &amp;&amp; <span class="hljs-built_in">cd</span> npm
</code></pre>
</li>
<li><p>Now create the <code>docker-compose.yml</code> file:</p>
<pre><code class="lang-yaml">  <span class="hljs-attr">version:</span> <span class="hljs-string">'3.8'</span>
  <span class="hljs-attr">services:</span>
    <span class="hljs-attr">app:</span>
      <span class="hljs-attr">image:</span> <span class="hljs-string">'jc21/nginx-proxy-manager:latest'</span>
      <span class="hljs-attr">restart:</span> <span class="hljs-string">unless-stopped</span>
      <span class="hljs-attr">ports:</span>
        <span class="hljs-comment"># These ports are in format &lt;host-port&gt;:&lt;container-port&gt;</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">'80:80'</span> <span class="hljs-comment"># Public HTTP Port</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">'443:443'</span> <span class="hljs-comment"># Public HTTPS Port</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">'81:81'</span> <span class="hljs-comment"># Admin Web Port</span>

      <span class="hljs-attr">environment:</span>
        <span class="hljs-comment"># Mysql/Maria connection parameters:</span>
        <span class="hljs-attr">DB_MYSQL_HOST:</span> <span class="hljs-string">"db"</span>
        <span class="hljs-attr">DB_MYSQL_PORT:</span> <span class="hljs-number">3306</span>
        <span class="hljs-attr">DB_MYSQL_USER:</span> <span class="hljs-string">"npm"</span>
        <span class="hljs-attr">DB_MYSQL_PASSWORD:</span> <span class="hljs-string">"npm"</span>
        <span class="hljs-attr">DB_MYSQL_NAME:</span> <span class="hljs-string">"npm"</span>
      <span class="hljs-attr">volumes:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">./data:/data</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">./letsencrypt:/etc/letsencrypt</span>
      <span class="hljs-attr">depends_on:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">db</span>

    <span class="hljs-attr">db:</span>
      <span class="hljs-attr">image:</span> <span class="hljs-string">'jc21/mariadb-aria:latest'</span>
      <span class="hljs-attr">restart:</span> <span class="hljs-string">unless-stopped</span>
      <span class="hljs-attr">environment:</span>
        <span class="hljs-attr">MYSQL_ROOT_PASSWORD:</span> <span class="hljs-string">'npm'</span>
        <span class="hljs-attr">MYSQL_DATABASE:</span> <span class="hljs-string">'npm'</span>
        <span class="hljs-attr">MYSQL_USER:</span> <span class="hljs-string">'npm'</span>
        <span class="hljs-attr">MYSQL_PASSWORD:</span> <span class="hljs-string">'npm'</span>
        <span class="hljs-attr">MARIADB_AUTO_UPGRADE:</span> <span class="hljs-string">'1'</span>
      <span class="hljs-attr">volumes:</span>
        <span class="hljs-bullet">-</span> <span class="hljs-string">./mysql:/var/lib/mysql</span>
</code></pre>
</li>
</ul>
<blockquote>
<p>💡 <strong>Why a database?</strong><br />The database stores all your proxy host settings, SSL metadata, users, and configuration changes persistently</p>
</blockquote>
<h3 id="heading-step-3-launch-the-container">Step 3: Launch the Container</h3>
<p>Run the containers:</p>
<pre><code class="lang-bash">docker-compose up -d
</code></pre>
<h3 id="heading-step-4-access-the-web-interface">Step 4: Access the Web Interface</h3>
<p>Open your browser and visit:<br /><code>http://your-server-ip:81</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750081729562/2645f332-454b-4411-8532-bf3d2956e1eb.png" alt class="image--center mx-auto" /></p>
<p>Login with default credentials:</p>
<ul>
<li><p><strong>Email:</strong> <code>admin@example.com</code></p>
</li>
<li><p><strong>Password:</strong> <code>changeme</code></p>
</li>
</ul>
<blockquote>
<p>You’ll be prompted to change your login details after the first login.</p>
</blockquote>
<p>Once you login, you’ll be redirected to your dashboard</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750081786945/6cdda54f-96fb-405a-9bcc-7b49c0d48959.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-setting-up-your-first-proxy-host">🔧 Setting Up Your First Proxy Host</h2>
<ol>
<li><p>Go to <strong>"Proxy Hosts" &gt; "Add Proxy Host"</strong></p>
</li>
<li><p>Enter your <strong>domain name</strong> (e.g., <a target="_blank" href="http://myapp.example.com"><code>npm.domain.com</code></a>)</p>
</li>
<li><p>Enter the <strong>internal IP address and port</strong> of your service (e.g., <code>192.168.1.100:8080</code>)</p>
</li>
<li><p>Enable <strong>"Block Common Exploits"</strong></p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750081961598/7c209051-2b7a-47fe-b208-48cbd57e601d.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Switch to the <strong>SSL</strong> tab:</p>
<ul>
<li><p>Enable <strong>SSL</strong></p>
</li>
<li><p>Request a <strong>Let’s Encrypt certificate</strong></p>
</li>
<li><p>Enable <strong>Force SSL</strong></p>
</li>
</ul>
</li>
</ol>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1750082023393/617953e9-3786-4b8e-9dd4-08f3014a0733.png" alt class="image--center mx-auto" /></p>
<ol start="6">
<li>Click <strong>Save</strong></li>
</ol>
<blockquote>
<p>🎉 Done! Your service is now live, secure, and reachable via your custom domain. (npm.domain.com in this case)</p>
</blockquote>
<h2 id="heading-advanced-features">🔐 Advanced Features</h2>
<ul>
<li><p><strong>Access Lists</strong> – Restrict access using IP whitelisting or basic auth</p>
</li>
<li><p><strong>Custom NGINX Directives</strong> – Add your own caching rules, headers, or security policies</p>
</li>
<li><p><strong>Rate Limiting</strong> – Throttle excessive traffic and protect against DoS attacks</p>
</li>
<li><p><strong>Redirection Hosts</strong> – Easily forward old URLs to new destinations</p>
</li>
<li><p><strong>404 Hosts</strong> – Define catch-all behavior for unknown or misrouted domains</p>
</li>
</ul>
<h2 id="heading-common-use-cases">💡 Common Use Cases</h2>
<ul>
<li><p>Host self-hosted apps like <strong>Nextcloud</strong>, <strong>Jellyfin</strong>, <strong>Bitwarden</strong>, or <strong>Portainer</strong></p>
</li>
<li><p>Expose internal microservices on custom subdomains</p>
</li>
<li><p>Make local development tools publicly accessible with HTTPS</p>
</li>
<li><p>Replace complex manual NGINX or Traefik setups with a user-friendly dashboard</p>
</li>
</ul>
<h2 id="heading-final-thoughts">🧠 Final Thoughts</h2>
<p>NGINX is incredibly powerful, but configuring it manually can be time-consuming and error-prone.<br /><strong>NGINX Proxy Manager</strong> simplifies this process without sacrificing flexibility.</p>
<p>It’s perfect for:</p>
<p>✅ Self-hosters<br />✅ Developers managing multiple projects<br />✅ Small teams hosting internal tools<br />✅ Anyone who wants <strong>easy SSL</strong> and <strong>quick proxy setup</strong></p>
]]></content:encoded></item><item><title><![CDATA[Getting started with Node.js]]></title><description><![CDATA[Node.js is a JavaScript runtime that extends its capability to the server side. It is built on Chrome’s V8 JavaScript Engine.
Node is an event-driven, non-blocking IO model. This means it's asynchronous and doesn't block itself for one request (but r...]]></description><link>https://blogs.arasharora.com/getting-started-with-nodejs</link><guid isPermaLink="true">https://blogs.arasharora.com/getting-started-with-nodejs</guid><category><![CDATA[Node.js]]></category><category><![CDATA[handlebars]]></category><category><![CDATA[backend]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Arash Arora]]></dc:creator><pubDate>Sun, 01 Jan 2023 11:52:59 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1672574343617/ec38ae8b-baa3-4afb-9bd7-a76dbd716d2a.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Node.js is a JavaScript runtime that extends its capability to the server side. It is built on Chrome’s V8 JavaScript Engine.</p>
<p>Node is an event-driven, non-blocking IO model. This means it's asynchronous and doesn't block itself for one request (but rather immediately moves to the next request). This makes Node extraordinarily fast and efficient.</p>
<p>Event-driven means that as soon as Node starts it initiates all the variables and functions and waits for an event to occur.</p>
<p>NPM stands for Node Package Manager which helps you manage your packages for Node. NPX stands for Node Package Execute, and it can execute any npm package without even installing it.</p>
<p>To download npm head to <a target="_blank" href="https://nodejs.org/en/download/">https://nodejs.org/en/download/</a>.</p>
<h2 id="heading-how-to-write-your-first-nodejs-program-hello-world"><strong>How to Write Your First Node.js Program (Hello World)</strong></h2>
<p>Create a file called hello_world.js in your project folder</p>
<p>Then open the file in your code editor like VS Code. Type the code <code>console.log(“Hello World”);</code> in your editor.</p>
<p>Open the terminal, and navigate to the file location.</p>
<p>Now type <code>node hello_world.js</code>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2022/06/image-196.png" alt="image-196" /></p>
<h2 id="heading-how-to-import-node-core-modules"><strong>How to Import Node Core Modules</strong></h2>
<p>So let’s start with the very basic package, which is <strong>fs (file system)</strong>. You use it to create, read, and modify files.</p>
<p>To import the fs module, type this command: <code>const fs = require(“fs”);</code>.</p>
<p>Now to use any function of this module, you can refer to the <a target="_blank" href="https://nodejs.org/docs/latest-v17.x/api/fs.html#file-system">docs</a>.</p>
<p>To create a file, we can use <code>fs.writeFileSync(filename, content);</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(“fs”);
fs.writeFileSync(“file.txt”, “Hi there..”);
</code></pre>
<p><img src="https://miro.medium.com/max/446/1*KzqmGo9SE7R3XPYOS-3LXg.png" alt="1*KzqmGo9SE7R3XPYOS-3LXg" /></p>
<p>To append anything in the same file we can use this:</p>
<pre><code class="lang-js">fs.appendFileSync(filename, content);.
</code></pre>
<p><img src="https://miro.medium.com/max/842/1*dOqUqcuJ5a5vl_BQ_E0dSg.png" alt="1*dOqUqcuJ5a5vl_BQ_E0dSg" /></p>
<h2 id="heading-how-to-install-npm-packages"><strong>How to Install NPM Packages</strong></h2>
<p>Now we will use a very basic npm package called <strong>superheroes</strong> (which is a list of random superheroes) to help you understand how npm works.</p>
<p>To install any npm package, we can use this command in the cmd:</p>
<pre><code class="lang-javascript">npm install superheroes
</code></pre>
<p>Now to import the installed package type <code>const sh = require(“superheroes”);</code>.</p>
<p>To display the name of a random superhero, use this command:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(sh.random());.
</code></pre>
<p><img src="https://miro.medium.com/max/1400/1*WfHNl2GDgyXBEwfV6oV0GQ.png" alt="1*WfHNl2GDgyXBEwfV6oV0GQ" /></p>
<p>Let’s try another package. Now we’ll install one of the most used npm packages called <strong>“chalk” —</strong> it styles text strings in the terminal.</p>
<p>To install the chalk package (we will install version 2.4.2 as it allows us to import the package using the <strong>require</strong> method), type the following command:</p>
<pre><code class="lang-js">npm install chalk@<span class="hljs-number">2.4</span><span class="hljs-number">.2</span>
</code></pre>
<p>Now to style the text string, use this command to pick the colour of the string:</p>
<pre><code class="lang-js">chalk.color(text)
</code></pre>
<p><img src="https://miro.medium.com/max/1400/1*AQ5TX0vxzPn5N0lzrSBbJw.png" alt="1*AQ5TX0vxzPn5N0lzrSBbJw" /></p>
<p>You can read more about the <a target="_blank" href="https://www.npmjs.com/package/chalk">chalk package here</a>.</p>
<h2 id="heading-how-to-initiate-npm-in-our-program"><strong>How to Initiate NPM in Our Program</strong></h2>
<p>To initiate NPM in our program, we can use the following command:</p>
<pre><code class="lang-js">npm init
</code></pre>
<p>Then press enter or answer the questions accordingly.</p>
<p><img src="https://miro.medium.com/max/1400/1*G_SVRqNdjuuWssQANvZgbw.png" alt="1*G_SVRqNdjuuWssQANvZgbw" /></p>
<p>Or you can directly use the command <code>npm init -y</code> (same as pressing enter on all the questions).</p>
<p><img src="https://miro.medium.com/max/1400/1*CafNbhzEhvGAayNHnpb29A.png" alt="1*CafNbhzEhvGAayNHnpb29A" /></p>
<p>This will result in the creation of the <strong>package.json</strong> file:</p>
<p><img src="https://miro.medium.com/max/1400/1*hYaMdTgcLdABQ1qqjQdpRQ.png" alt="1*hYaMdTgcLdABQ1qqjQdpRQ" /></p>
<h3 id="heading-so-what-is-packagejson"><strong>So, what is package.json?</strong></h3>
<p>package.json is the heart of any Nodejs project. It maintains a record of all the dependencies (NPM packages) and contains the metadata of every project.</p>
<p>If someone else downloads the project, this file will help them install all the dependencies required to run the program.</p>
<h2 id="heading-how-to-use-momentjs-an-npm-package"><strong>How to Use Moment.js — an NPM Package</strong></h2>
<p>This is one of the most used npm packages out there. You can use it to parse and validate dates.</p>
<p>To install the package, run this command:</p>
<pre><code class="lang-js">npm i moment
</code></pre>
<p>Import the package like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moment = <span class="hljs-built_in">require</span>(“moment”);
</code></pre>
<p>To create a Date object to fetch the current date and time (JavaScript method), run this code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> time = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>();
</code></pre>
<p>Now, to parse or format the date we will use the <strong>moment</strong> package:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> parsedTime = moment(time).format(<span class="hljs-string">"h:mm:ss"</span>);
</code></pre>
<p>Print the parsed time like this:</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(parsedTime);
</code></pre>
<p><img src="https://miro.medium.com/max/1400/1*V3hJ24cmTASx9k6Rv83gXg.png" alt="1*V3hJ24cmTASx9k6Rv83gXg" /></p>
<p>This is the package.json for this project which includes all the dependency packages — <strong>moment</strong> in this case.</p>
<p><img src="https://miro.medium.com/max/1400/1*kKFpiaEOtsRbxN67do4HDw.png" alt="1*kKFpiaEOtsRbxN67do4HDw" /></p>
<p>We also have <strong>node_modules</strong> in the project folder. This folder contains all the dependencies our project depends on including moment and other packages that moment depends on.</p>
<p><img src="https://miro.medium.com/max/454/1*-mxxdXnGzLxG98LE2ebMDQ.png" alt="1*-mxxdXnGzLxG98LE2ebMDQ" /></p>
<p><strong>package-lock.json</strong> is another file in our project folder that contains all the information regarding the name, dependencies, version of dependencies, and locked version of the project.</p>
<p>It describes the exact tree that was generated to allow subsequent installs to have the identical tree.</p>
<p><img src="https://miro.medium.com/max/1400/1*b1VMBTQ3HtQtnaHUWGY8iQ.png" alt="1*b1VMBTQ3HtQtnaHUWGY8iQ" /></p>
<h1 id="heading-how-to-use-express-js-a-nodejs-framework"><strong>How to Use Express JS — a NodeJS Framework</strong></h1>
<p>Express is a Node.js web application framework that offers a comprehensive range of functionality for both web and mobile apps.</p>
<h3 id="heading-how-to-install-express"><strong>How to install Express</strong></h3>
<p>To install Express, run this command:</p>
<pre><code class="lang-js">npm install express
</code></pre>
<p>Then you'll need to import Express like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
</code></pre>
<h3 id="heading-how-to-create-an-express-application"><strong>How to create an Express application</strong></h3>
<p>To create an Express app, just run this command:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> app = express()
</code></pre>
<h3 id="heading-how-to-start-a-server-on-port-3000"><strong>How to start a server on port 3000</strong></h3>
<pre><code class="lang-js">app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">() =&gt;</span> { 
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server running on port 3000"</span>);
}
</code></pre>
<p><img src="https://miro.medium.com/max/1400/1*jD3FvRLcd_j2MuZ0U6_bXw.png" alt="1*jD3FvRLcd_j2MuZ0U6_bXw" /></p>
<p>Now you can open <a target="_blank" href="http://localhost:3000"><strong>http://localhost:3000</strong></a> to reach your created server</p>
<p><img src="https://miro.medium.com/max/844/1*IMLmUArtV_ctmiAG18TnJg.png" alt="1*IMLmUArtV_ctmiAG18TnJg" /></p>
<p>Okay, so “cannot get /” means that there is no defined route “/” yet.</p>
<p>So to define the “/” route we use <code>app.get()</code> the function.</p>
<p>The <strong>app.get (route, callback function)</strong> function is used to handle all GET requests.</p>
<p>The callback function has two arguments, <strong>req</strong> and <strong>res</strong>, which refer to HTTP requests and the desired response, respectively. The argument names (req, res) are not fixed and can be named anything you want.</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req,res</span>) =&gt;</span> { 
    <span class="hljs-comment">// app.get to handle GET requests</span>
    <span class="hljs-comment">// req - http request, res - desired response</span>
    res.send(<span class="hljs-string">"Hello World"</span>); <span class="hljs-comment">// send Hello World to this route</span>
}
</code></pre>
<h2 id="heading-how-to-create-a-hello-world-program-in-express"><strong>How to Create a Hello World Program in Express</strong></h2>
<p>In this section, we will create the very basic program of Hello World in Express.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> app = express();
app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {  
    res.send(<span class="hljs-string">"hello world"</span>);
});
app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">() =&gt;</span> {  
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server running on 3000"</span>);
});
</code></pre>
<p>And here's the output</p>
<p><img src="https://miro.medium.com/max/1060/1*uRqmENgESv8cdq-0oSaX8A.png" alt="1*uRqmENgESv8cdq-0oSaX8A" /></p>
<h2 id="heading-how-to-render-static-files-in-express"><strong>How to Render Static Files in Express</strong></h2>
<p>This section introduces us to the concept of static file rendering using Express.</p>
<p>First, you'll need to create a new project folder. Then you'll initialize npm using <code>npm init -y</code>.</p>
<p>Install the Express package <code>npm i express</code> and create a file called app.js.</p>
<p>Then you'll create an app, and listen or start the server on port 3000.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express);
const app = express();
app.listen(3000, () =&gt; {  
    console.log("</span>Server running on <span class="hljs-number">3000</span><span class="hljs-string">");
}</span>
</code></pre>
<p>To render static web pages such as HTML, CSS, and JS, create a folder named public in the root directory.</p>
<p>As we’re focusing on only the backend, we will not spend much time on the frontend and will create only an HTML file in the public folder.</p>
<p><img src="https://miro.medium.com/max/1142/1*-OiGmKZaz7GKc3NdNVjZdg.png" alt="1*-OiGmKZaz7GKc3NdNVjZdg" /></p>
<p>We will now import the <strong>path</strong> module and join the specified paths into one:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(“path”);
</code></pre>
<p>Now to render these files, we have to use the following command:</p>
<pre><code class="lang-js">app.use(express.static(path.join(__dirname, “/public”)));
</code></pre>
<p><strong>__dirname →</strong> returns the current directory</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> express();
app.use(express.static(path.join(__dirname, <span class="hljs-string">"/public"</span>)));
app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">() =&gt;</span> {  
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server running on 3000"</span>);
});
</code></pre>
<p>And here's the output:</p>
<p><img src="https://miro.medium.com/max/1034/1*2U5Qi3XKOaNF0MjXSTo0tg.png" alt="1*2U5Qi3XKOaNF0MjXSTo0tg" /></p>
<h2 id="heading-how-to-render-dynamic-files-in-express"><strong>How to Render Dynamic Files in Express</strong></h2>
<p>In this section, we will learn to render dynamic files in which we can use values from an input object.</p>
<p>To render dynamic web pages, there are many templates such as pug, handlebars, ejs, and so on. These templates allow us to inject dynamic data, if conditions, and loops at runtime.</p>
<p>But here we will focus on handlebars.</p>
<p>Install the packages (express and hbs):</p>
<pre><code class="lang-js">npm i hbs express
</code></pre>
<p>Create a file named app.js and import the packages like this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(“express”);
<span class="hljs-keyword">const</span> hbs = <span class="hljs-built_in">require</span>(“hbs”);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(“path”);
</code></pre>
<p>Create an Express app and listen on port 3000:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> app = express();
app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">(<span class="hljs-params">req,res</span>) =&gt;</span> {  
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server running on 3000"</span>);
}
</code></pre>
<p>For handlebars to work, we need to set the view engine as hbs.</p>
<pre><code class="lang-js">app.set(“view engine”, “hbs”);
</code></pre>
<p>The view engine enables us to render dynamic webpages using the specified template.</p>
<p>The view engine generally looks for the “views” folder in the root folder. But to avoid errors we’ll mention the path of “views” using the following command:</p>
<pre><code class="lang-js">app.set(“views”, path.join(__dirname,“/views”);
</code></pre>
<p>Now create a <strong>views</strong> folder in the root directory. Under that create a file called index.hbs (.hbs is the extension of handlebars) and insert the following HTML code:</p>
<h3 id="heading-indexhbs"><strong>index.hbs</strong></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> 
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Dynamic Rendering<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> 
    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>  
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Dynamic Rendering<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>   
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{{author}}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span> <span class="hljs-comment">&lt;!--dynamic data recieved from server--&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p><code>{{author}}</code> — it is the syntax to insert dynamic data</p>
<p>Now to render the index.hbs file we’ll create an app.get function to handle the GET request on the route “/” and send the dynamic data <strong>author</strong>.</p>
<pre><code class="lang-js">app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> { 
    res.render(<span class="hljs-string">"index"</span>, {    
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Arash Arora"</span>, 
    });
});
</code></pre>
<p><code>res.render</code> is the function to render the view. Here we have to pass two arguments. The first is the name of the file without the extension and the second is the object of local variables, for example, the <strong>author</strong>.</p>
<h3 id="heading-appjs-file"><strong>app.js file</strong></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> hbs = <span class="hljs-built_in">require</span>(<span class="hljs-string">"hbs"</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);
<span class="hljs-keyword">const</span> app = express();
app.set(<span class="hljs-string">"view engine"</span>, <span class="hljs-string">"hbs"</span>);
app.set(<span class="hljs-string">"views"</span>, path.join(__dirname, <span class="hljs-string">"/views"</span>));
app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {  
    res.render(<span class="hljs-string">"index"</span>, {    
        <span class="hljs-attr">author</span>: <span class="hljs-string">"Arash Arora"</span>, 
    });
});
app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> { 
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server listening on 3000"</span>);
});
</code></pre>
<h3 id="heading-folder-structure"><strong>Folder structure</strong></h3>
<p><img src="https://miro.medium.com/max/502/1*7xz9Fj17mTS5pZhxzf2dvw.png" alt="1*7xz9Fj17mTS5pZhxzf2dvw" /></p>
<p>And here's the output:</p>
<p><img src="https://miro.medium.com/max/824/1*JQt1mgjLTU-LJJ0XS7UH3A.png" alt="1*JQt1mgjLTU-LJJ0XS7UH3A" /></p>
<h1 id="heading-how-to-create-advanced-templates-with-handlebars"><strong>How to Create Advanced Templates with Handlebars</strong></h1>
<p>So, this is where we’ll learn about reusable components. Previously, we had to construct identical components for each and every page when it came to headers and footers.</p>
<p>But because there are so many repetitive tasks, advanced templating is the saviour. This concept states that we will just make one component that will be used everywhere we need it.</p>
<h3 id="heading-handlebars-introduced-the-concept-of-partials"><strong>Handlebars introduced the concept of Partials</strong></h3>
<p>Partials are the regular handlebar files that other templates can call. Partials are a widely used templating concept that is not specific to Handlebars.</p>
<p>To construct templates that are likely to be reused, you can isolate them into their files (a Partial), and then use them in various templates. You can consider partials to be a simple technique for modularizing your templates.</p>
<p>Follow these steps to create partials:</p>
<ul>
<li><p>Initiate npm → <code>npm init -y</code></p>
</li>
<li><p>Install the required packages, Express, and hbs → <code>npm i express hbs</code></p>
</li>
<li><p>Create your folder templates</p>
</li>
<li><p>Create two additional folders within the folder templates: <strong>partials and views</strong></p>
</li>
<li><p>Now create a file <strong>app.js</strong></p>
</li>
</ul>
<p><img src="https://miro.medium.com/max/472/1*98jLDll1IWq-vd8H0ieNCg.png" alt="1*98jLDll1IWq-vd8H0ieNCg" /></p>
<p>The folder structure should be similar</p>
<p>Let’s create two partial files: header.hbs and footer.hbs. And we'll also add two views, index.hbs and about.hbs.</p>
<p><img src="https://miro.medium.com/max/422/1*E32yq-EHCLFfUFzbgIbJJg.png" alt="1*E32yq-EHCLFfUFzbgIbJJg" /></p>
<h3 id="heading-indexhbs-1"><strong>index.hbs</strong></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>   
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Advanced Templating<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>  
    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>    
        {{&gt;header}} <span class="hljs-comment">&lt;!--include the header component--&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>I'm a savior<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>    
        {{&gt;footer}} <span class="hljs-comment">&lt;!-- include the footer component --&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-abouthbs"><strong>about.hbs</strong></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>    
        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Advanced Templating -- About<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> 
    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>   
        {{&gt;header}}   
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Handlebars<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>    
        {{&gt;footer}} 
    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-headerhbs"><strong>header.hbs</strong></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Advanced Templating<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span> 
    <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>{{title}}<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span><span class="hljs-comment">&lt;!--dynamic data received from server--&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span> 
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"/about"</span>&gt;</span>About<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
</code></pre>
<h3 id="heading-footerhbs"><strong>footer.hbs</strong></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span>  
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Created by {{name}}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span> <span class="hljs-comment">&lt;!--name -&gt; dynamic data --&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
</code></pre>
<h3 id="heading-appjs"><strong>app.js</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">"express"</span>);
<span class="hljs-keyword">const</span> hbs = <span class="hljs-built_in">require</span>(<span class="hljs-string">"hbs"</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">"path"</span>);
<span class="hljs-keyword">const</span> app = express();
app.set(<span class="hljs-string">"view engine"</span>, <span class="hljs-string">"hbs"</span>);
app.set(<span class="hljs-string">"views"</span>, path.join(__dirname, <span class="hljs-string">"/templates/views"</span>));
hbs.registerPartials(path.join(__dirname, <span class="hljs-string">"/templates/partials"</span>));
app.get(<span class="hljs-string">"/"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {  
    res.render(<span class="hljs-string">"index"</span>, {    
        <span class="hljs-attr">title</span>: <span class="hljs-string">"Home"</span>,    
        <span class="hljs-attr">name</span>: <span class="hljs-string">"Arash Arora"</span>,  
    });
});
app.get(<span class="hljs-string">"/about"</span>, <span class="hljs-function">(<span class="hljs-params">req, res</span>) =&gt;</span> {  
    res.render(<span class="hljs-string">"about"</span>, {    
        <span class="hljs-attr">title</span>: <span class="hljs-string">"About"</span>,    
        <span class="hljs-attr">name</span>: <span class="hljs-string">"Arash Arora"</span>,  
    });
});
app.listen(<span class="hljs-number">3000</span>, <span class="hljs-function">() =&gt;</span> {  
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Listening on port 3000"</span>);
});
</code></pre>
<p>Everything is the same as I explained in the section on rendering dynamic files in Express – except here we have to <strong>register the partials</strong> to use partials.</p>
<h3 id="heading-how-to-register-partials"><strong>How to register partials</strong></h3>
<pre><code class="lang-js">hbs.registerPartials(path_to_partials)
</code></pre>
<p>As we’ve created the partials directory in the templates folder, here is the path of partials:</p>
<pre><code class="lang-js">hbs.registerPartials(path.join(__dirname, <span class="hljs-string">"/templates/partials"</span>));
</code></pre>
<hr />
<h1 id="heading-wrapping-up"><strong>Wrapping Up</strong></h1>
<p>In this article, we've covered Node.js from theory to practice. Although Node.js is a vast topic that you can't learn entirely from a single shorter article, I've done my best to cover some of the essential features to help you get started with the journey.</p>
<p>In a nutshell, we've discussed what Node.js is, which is a non-blocking, event-driven JavaScript runtime that is asynchronous and uses a single thread to perform operations. We've also discussed the most used minimal and flexible Node.js web application framework, Express.</p>
<p>Then we talked about Node.js's NPM, NPX, and Static and Dynamic data rendering.</p>
<p>All in all, Node.js is a fantastic technology to know, and the possibilities are endless because of its vast community.</p>
]]></content:encoded></item><item><title><![CDATA[Getting started with Next.js]]></title><description><![CDATA[Introduction to Next.js
Next.js is a robust JavaScript framework that has gained a lot of popularity in recent years due to its focus on server-rendered React applications and its strong emphasis on performance. If you're a developer looking to build...]]></description><link>https://blogs.arasharora.com/getting-started-with-nextjs</link><guid isPermaLink="true">https://blogs.arasharora.com/getting-started-with-nextjs</guid><category><![CDATA[dynamic site generation]]></category><category><![CDATA[Next.js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[static site generation]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Arash Arora]]></dc:creator><pubDate>Sun, 01 Jan 2023 11:44:50 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/75cca40e28deca4375248c9251a7517b.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction-to-nextjs">Introduction to Next.js</h2>
<p>Next.js is a robust JavaScript framework that has gained a lot of popularity in recent years due to its focus on server-rendered React applications and its strong emphasis on performance. If you're a developer looking to build fast, scalable, and easy-to-maintain web applications, Next.js is definitely worth taking a look at.</p>
<p>In this article, we'll give you a comprehensive introduction to Next.js, including what it is, how it differs from other JavaScript frameworks, and some of its key features. By the end of this article, you'll have a good understanding of what Next.js is and how it can be used to build high-quality web applications.</p>
<h2 id="heading-setting-up-a-nextjs-project">Setting up a Next.js project</h2>
<p>Once you've decided that you want to use Next.js for your project, the first thing you'll need to do is set up a new Next.js project. Here's how you can do that:</p>
<ol>
<li>Installing Next.js and creating a new project The first step is to install Next.js itself. You can do this by running the following command in your terminal:</li>
</ol>
<pre><code class="lang-plaintext">npm install --save next react react-dom
</code></pre>
<p>This will install Next.js, as well as the React and ReactDOM libraries which are required for Next.js to work.</p>
<p>Next, you can create a new Next.js project by running the following command:</p>
<pre><code class="lang-plaintext">npx create-next-app my-app
</code></pre>
<p>This will create a new directory called "my-app" containing a basic Next.js project. You can replace "my-app" with the name you want to give your project.</p>
<ol>
<li>The directory structure of a Next.js project Once you've created your Next.js project, you'll see that it has the following basic directory structure:</li>
</ol>
<pre><code class="lang-plaintext">my-app/
├── pages/
├── public/
├── package.json
├── .gitignore
└── README.md
</code></pre>
<ul>
<li><p>The "pages" directory is where you'll put the React components that make up your application. Each file in the "pages" directory represents a page in your application, and the file's name corresponds to that page's route.</p>
</li>
<li><p>The "public" directory is where you can put any static assets that you want to be available to your application, such as images or fonts.</p>
</li>
<li><p>The "package.json" file contains metadata about your project, including dependencies and scripts.</p>
</li>
<li><p>The ".gitignore" file lists files you don't want to include in your Git repository.</p>
</li>
<li><p>The "<a target="_blank" href="http://README.md">README.md</a>" file is a markdown file that you can use to provide information about your project.</p>
</li>
</ul>
<ol>
<li>Customizing the Next.js configuration Next.js has a number of configuration options that you can set to customize the behaviour of your application. You can find these options in the "next.config.js" file at the root of your project. Some of the options that you might want to configure include:</li>
</ol>
<ul>
<li><p>"target": This option specifies the environment that your application will be running in. Possible values include "server" (for server-rendered applications), "serverless" (for applications that use serverless functions), and "experimental-serverless-trace" (for applications that use serverless functions with performance tracing).</p>
</li>
<li><p>"webpack": This option allows you to customize the Webpack configuration that Next.js uses to build your application.</p>
</li>
<li><p>"serverRuntimeConfig": This option allows you to specify configuration options that are only available on the server side of your application.</p>
</li>
</ul>
<p>By default, Next.js comes with a set of reasonable defaults for these options, but you can override them as needed to suit your specific needs.</p>
<p>In this section, we've covered the basics of setting up a Next.js project. In the next section, we'll look at how to create pages and routes in Next.js.</p>
<h2 id="heading-building-a-nextjs-application">Building a Next.js application</h2>
<p>Now that you've set up your Next.js project, it's time to start building your application. In this section, we'll cover some of the key concepts that you'll need to know to build a Next.js app, including creating pages and routes, fetching data, and adding styles and assets.</p>
<ol>
<li><p><strong>Creating pages and routes in Next.js</strong></p>
<p> In Next.js, each page in your application corresponds to a file in the "pages" directory. The name of the file determines the route of the page. For example, if you create a file called "about.js" in the "pages" directory, it will be available at the "<a target="_blank" href="http://localhost:3000/about"><strong>http://localhost:3000/about</strong></a>" route.</p>
</li>
</ol>
<p>Here's a simple example of a page in Next.js:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">About</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>About page<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
}
</code></pre>
<p>You can also create dynamic routes by using named parameters in the file name. For example, if you create a file called "post[id].js", it will be available at the "<a target="_blank" href="http://localhost:3000/post/:id"><strong>http://localhost:3000/post/:id</strong></a>" route, and you can access the "id" parameter in the page component like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Post</span>(<span class="hljs-params">{ id }</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Post {id}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
}
</code></pre>
<ol>
<li><p><strong>Fetching data with Next.js</strong></p>
<p> Next.js has a built-in mechanism for fetching data called "getStaticProps" and "getServerSideProps". These functions allow you to fetch data at build time (for static sites) or on each request (for server-rendered sites).</p>
</li>
</ol>
<p>Here's an example of how you can use "getStaticProps" to fetch data for a page:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">export</span> <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getStaticProps</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Fetch data from an external API</span>
  <span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'https://api.example.com/posts'</span>)
  <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> res.json()

  <span class="hljs-comment">// Pass data to the page via props</span>
  <span class="hljs-keyword">return</span> { <span class="hljs-attr">props</span>: { data } }
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Posts</span>(<span class="hljs-params">{ data }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {data.map((post) =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{post.id}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  )
}
</code></pre>
<ol>
<li><p><strong>Adding styles and assets to a Next.js project</strong></p>
<p> Next.js uses Webpack to build and bundle your application, and you can use Webpack's built-in support for loading assets to include styles and other assets in your application.</p>
</li>
</ol>
<p>To include a CSS file in your application, you can use the "import" statement in your JavaScript code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> <span class="hljs-string">'./style.css'</span>
</code></pre>
<p>You can also include images and other assets by using the "import" statement or the "public" directory. For example, you can include an image like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> logo <span class="hljs-keyword">from</span> <span class="hljs-string">'./logo.png'</span>

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Home</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{logo}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Logo"</span> /&gt;</span></span>
}
</code></pre>
<p>That's it for this section! In the next section, we'll look at how to deploy your Next.js application.</p>
<h2 id="heading-deploying-a-nextjs-application">Deploying a Next.js application</h2>
<p>Once you've built your Next.js application, the next step is to deploy it so that others can access it. In this section, we'll cover some of the options for deploying a Next.js app, as well as some best practices for making sure your deployment goes smoothly.</p>
<ol>
<li><p><strong>Options for deploying a Next.js app</strong></p>
<p> There are a number of options for deploying a Next.js app, including:</p>
</li>
</ol>
<ul>
<li><p><strong>Deploying to a traditional server:</strong> You can deploy your Next.js app to a traditional server just like any other Node.js application. This requires setting up a server with Node.js and a web server such as NGINX and then deploying your application to it.</p>
</li>
<li><p><strong>Deploying to a cloud platform:</strong> You can also deploy your Next.js app to a cloud platform such as Heroku, AWS, or GCP. This is usually the easiest option, as the cloud provider will handle setting up the server and installing the necessary dependencies for you.</p>
</li>
<li><p><strong>Deploying as a static site:</strong> If your Next.js app is primarily a static site (i.e., it doesn't have any server-side logic), you can use Next.js's built-in static site generation feature to generate a set of static HTML, CSS, and JavaScript files that you can then deploy to a CDN or hosting provider.</p>
</li>
</ul>
<ol>
<li><p><strong>Deploying to a serverless environment with Next.js</strong></p>
<p> Next.js also includes support for deploying your application to a serverless environment, such as AWS Lambda or Google Cloud Functions. This is a good option if you want to build a highly scalable application that can handle a large number of requests without incurring high costs.</p>
</li>
</ol>
<p>To deploy your Next.js app to a serverless environment, you'll need to use the "serverless" target in your "next.config.js" file:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">module</span>.exports = {
  <span class="hljs-attr">target</span>: <span class="hljs-string">'serverless'</span>
}
</code></pre>
<p>You'll also need to configure your cloud provider to run your application in a serverless environment. Check the documentation for your cloud provider for more information on how to do this.</p>
<ol>
<li><strong>Best practices for deploying a Next.js app Here are a few best practices to keep in mind when deploying a Next.js app:</strong></li>
</ol>
<ul>
<li><p><strong>Test your application thoroughly before deploying:</strong> Make sure to test your application thoroughly on your local machine before deploying it to production. This will help you catch any issues early and prevent them from affecting your users.</p>
</li>
<li><p><strong>Use a continuous integration/continuous deployment (CI/CD) pipeline:</strong> A CI/CD pipeline can help automate the process of building, testing, and deploying your application, which can save you a lot of time and effort.</p>
</li>
<li><p><strong>Use a staging environment:</strong> It's a good idea to set up a staging environment where you can test new features and changes before deploying them to production. This will allow you to catch any issues before they affect your users.</p>
</li>
<li><p><strong>Monitor your application:</strong> Once your application is deployed, make sure to set up monitoring to keep track of its performance and uptime. This will help you catch any issues early and keep your application running smoothly.</p>
</li>
</ul>
<p>That's it for this section! In the next section, we'll look at some advanced Next.js features that you can use to build even more powerful applications.</p>
<h2 id="heading-advanced-nextjs-features">Advanced Next.js features</h2>
<p>In this section, we'll look at some advanced Next.js features that you can use to build even more powerful applications. These features include static site generation, serverless functions, and using Next.js with a headless CMS.</p>
<ol>
<li><p><strong>Static site generation with Next.js</strong></p>
<p> Next.js includes a built-in static site generation feature that allows you to generate a set of static HTML, CSS, and JavaScript files from your application. This is a good option if you want to build a highly performant, statically-generated site that can be deployed to a CDN or hosting provider.</p>
</li>
</ol>
<p>To use static site generation in Next.js, you'll need to use the "export" command to generate the static files for your application. For example, you can run the following command to generate the static files for your application:</p>
<pre><code class="lang-javascript">next <span class="hljs-keyword">export</span>
</code></pre>
<p>This will create an "out" directory containing the static files for your application. You can then deploy these files to a CDN or hosting provider to serve your site.</p>
<ol>
<li><p><strong>Serverless functions with Next.js</strong></p>
<p> As we saw in the previous section, Next.js also includes support for deploying your application to a serverless environment using serverless functions. Serverless functions are a good option if you want to build a highly scalable application that can handle a large number of requests without incurring high costs.</p>
</li>
</ol>
<p>To use serverless functions in Next.js, you'll need to use the "serverless" target in your "next.config.js" file and write your functions as described in the previous section.</p>
<ol>
<li><p><strong>Using Next.js with a headless CMS</strong></p>
<p> A headless CMS is a content management system that exposes its content through an API, allowing you to build custom front-ends for your content. This is a good option if you want to use</p>
</li>
<li><p><strong>Some of the best Content Management System (CMS) that integrates perfectly with Next.js.</strong></p>
<ul>
<li><p><a target="_blank" href="https://www.sanity.io/">Sanity.io</a></p>
</li>
<li><p><a target="_blank" href="https://strapi.io/">Strapi</a></p>
</li>
<li><p><a target="_blank" href="https://prismic.io/">Prismic.io</a></p>
</li>
<li><p><a target="_blank" href="https://ghost.org/">Ghost</a></p>
</li>
<li><p><a target="_blank" href="https://enonic.com/">Enonic</a></p>
</li>
</ul>
</li>
</ol>
<pre><code>---

## Conclusion

In <span class="hljs-built_in">this</span> article, we<span class="hljs-string">'ve introduced Next.js, a powerful JavaScript framework for building server-rendered and statically-generated applications. We'</span>ve covered some <span class="hljs-keyword">of</span> the key features <span class="hljs-keyword">of</span> Next.js, including its ability to create pages and routes, fetch data, and add styles and assets to your application. We<span class="hljs-string">'ve also looked at options for deploying your Next.js app, including deploying to a serverless environment and using advanced features such as static site generation and serverless functions.

To recap, some of the benefits of using Next.js include:

* **Simplified server-side rendering:** Next.js makes it easy to build server-rendered applications, with automatic code splitting and optimized performance.

* **Static site generation:** Next.js allows you to generate a set of static HTML, CSS, and JavaScript files from your application, making it easy to build highly performant, statically-generated sites.

* **Serverless functions:** Next.js includes support for deploying your application to a serverless environment using serverless functions, allowing you to build highly scalable applications.


Looking to the future, there are a number of exciting developments in the Next.js framework. For example, the Next.js team is working on adding support for incremental static regeneration, which will allow you to update the static files for your site without rebuilding the entire site. They'</span>re also working on improving the developer experience, <span class="hljs-keyword">with</span> features such <span class="hljs-keyword">as</span> improved TypeScript support and better-debugging tools.

If you<span class="hljs-string">'re interested in learning more about Next.js, there are a number of resources available online. The Next.js documentation is a great place to start, and there are also a number of tutorials and courses available online that can help you get up to speed with the framework. Additionally, the Next.js community is active on social media and online forums, so don'</span>t hesitate to reach out <span class="hljs-keyword">if</span> you have any questions or need help <span class="hljs-keyword">with</span> your Next.js project.

That<span class="hljs-string">'s it for this article! We hope you'</span>ve learned something <span class="hljs-keyword">new</span> about Next.js and are inspired to start building your own applications <span class="hljs-keyword">with</span> the framework.
</code></pre>]]></content:encoded></item></channel></rss>