<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ShadyAlefrangy</title>
    <description>The latest articles on DEV Community by ShadyAlefrangy (@shadyalefrangy).</description>
    <link>https://dev.to/shadyalefrangy</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%2F867045%2Fed9e7c4b-504d-42ec-94b0-f5ad36f09eb5.png</url>
      <title>DEV Community: ShadyAlefrangy</title>
      <link>https://dev.to/shadyalefrangy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shadyalefrangy"/>
    <language>en</language>
    <item>
      <title>How To Use EJS to Template Your Node Application with nodejs pure</title>
      <dc:creator>ShadyAlefrangy</dc:creator>
      <pubDate>Tue, 24 May 2022 16:35:00 +0000</pubDate>
      <link>https://dev.to/shadyalefrangy/how-to-use-ejs-to-template-your-node-application-with-nodejs-pure-3i26</link>
      <guid>https://dev.to/shadyalefrangy/how-to-use-ejs-to-template-your-node-application-with-nodejs-pure-3i26</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi,&lt;/strong&gt;&lt;br&gt;
today I will explain how to use node.js pure to make this following:&lt;/p&gt;

&lt;p&gt;_1. Create new Web Server.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create 3 separate HTML pages with different content.&lt;/li&gt;
&lt;li&gt;Create two more pages header.html and footer.html as pages layout.&lt;/li&gt;
&lt;li&gt;Create a request handler that returns the content of one of these pages based on URL pathname.&lt;/li&gt;
&lt;li&gt;Each page return consists of header.html page then page content then footer.html._&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  1. Create new Web Server
&lt;/h2&gt;

&lt;p&gt;// create the main file with .mjs extension like this (main.mjs)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Import ES Modules

import http from 'http';
import fs from "fs/promises";
import path from 'path';
import { fileURLToPath, URL } from 'url';

// __dirname not included is ES Modules
const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Paths for layout files
const headerPath = path.join(__dirname, 'header.html');
const footerPath = path.join(__dirname, 'footer.html');

// Set port as constant
const port = 5000;

// Now create web server
const server = http.createServer(async (req, res) =&amp;gt; {});

// Run the server
server.listen(port, () =&amp;gt; {
    console.clear();
    console.log(`Server is running... at http://localhost:${port}`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Create 3 separate HTML pages with different content
&lt;/h2&gt;

&lt;p&gt;// create the first page (index.html)&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
    {#header#}
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;This is index Page&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod odit quis debitis quos suscipit enim? Officiis, eum! Tempora voluptates libero perspiciatis laudantium iusto animi suscipit? Quam illo nihil minima ut, in facere aspernatur, repellendus sit, laudantium cupiditate suscipit? Facilis veniam aspernatur iusto sunt necessitatibus aliquid accusamus voluptatum eos! Atque, unde!
    &amp;lt;/p&amp;gt;
    {#footer#}
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// create the second page&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
    {#header#}
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;This is about Page&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod odit quis debitis quos suscipit enim? Officiis, eum! Tempora voluptates libero perspiciatis laudantium iusto animi suscipit? Quam illo nihil minima ut, in facere aspernatur, repellendus sit, laudantium cupiditate suscipit? Facilis veniam aspernatur iusto sunt necessitatibus aliquid accusamus voluptatum eos! Atque, unde!
    &amp;lt;/p&amp;gt;
    {#footer#}
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// Create the third page&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
    {#header#}
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;This is users Page&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod odit quis debitis quos suscipit enim? Officiis, eum! Tempora voluptates libero perspiciatis laudantium iusto animi suscipit? Quam illo nihil minima ut, in facere aspernatur, repellendus sit, laudantium cupiditate suscipit? Facilis veniam aspernatur iusto sunt necessitatibus aliquid accusamus voluptatum eos! Atque, unde!
    &amp;lt;/p&amp;gt;
    {#footer#}
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Create two more pages header.html and footer.html as pages layout
&lt;/h2&gt;

&lt;p&gt;// create the header page.&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;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Node JS Server&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;// create the footer page&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;footer&amp;gt;
    &amp;lt;p&amp;gt;Author: Shady Alefrangy&amp;lt;/p&amp;gt;
    &amp;lt;p&amp;gt;&amp;lt;a href="example@example.com"&amp;gt;example@example.com&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;/footer&amp;gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Create a request handler that returns the content of one of these pages based on URL pathname.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  5. Each page return consists of header.html page then page content then footer.html
&lt;/h2&gt;

&lt;p&gt;I code this with asynchronous approach with (async/await)&lt;br&gt;
const server = http.createServer(async (req, res) =&amp;gt; {&lt;br&gt;
    const url = new URL(&lt;code&gt;http:${req.headers.host}${req.url}&lt;/code&gt;);&lt;br&gt;
    const fileName = url.pathname;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const renderedFilePath = path.join(__dirname, 'public', `${fileName}`);

try {
    await fs.access(renderedFilePath)
    let header = await fs.readFile(headerPath, {encoding: 'utf-8'});
    let footer = await fs.readFile(footerPath, {encoding: 'utf-8'});
    let content = await fs.readFile(renderedFilePath, {encoding: 'utf-8'});
    content = content.replace('{#header#}', header);
    content = content.replace('{#footer#}', footer);
    res.end(content);
} catch {
    res.statusCode = 404;
    res.end('File not found');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
