<?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: TechSynthesis</title>
    <description>The latest articles on Forem by TechSynthesis (@techsynthesis).</description>
    <link>https://forem.com/techsynthesis</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%2F748618%2Ff8d7e81a-7bd5-447a-be3d-e713956a14b6.png</url>
      <title>Forem: TechSynthesis</title>
      <link>https://forem.com/techsynthesis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/techsynthesis"/>
    <language>en</language>
    <item>
      <title>Format date in JS/React without any Library.</title>
      <dc:creator>TechSynthesis</dc:creator>
      <pubDate>Thu, 11 Nov 2021 14:41:52 +0000</pubDate>
      <link>https://forem.com/techsynthesis/format-date-in-jsreact-without-any-library-4352</link>
      <guid>https://forem.com/techsynthesis/format-date-in-jsreact-without-any-library-4352</guid>
      <description>&lt;p&gt;This is a simple utility function that I wrote for my Ecommerce Dashboard it takes the unformatted standard ISO 8601 date time string and formats it into a more readable format. &lt;/p&gt;

&lt;p&gt;Something like this &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F0qmx6Wv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1tay0aq8ewlkx3s7a1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F0qmx6Wv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1tay0aq8ewlkx3s7a1z.png" alt="readable date format" width="880" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So lets get started. &lt;br&gt;
Create a new .js file called&lt;code&gt;getDateHandler.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function getDateHandler(date) {
    const months = [
        "Jan",
        "Feb",
        "Mar",
        "Apr",
        "May",
        "Jun",
        "Jul",
        "Aug",
        "Sep",
        "Oct",
        "Nov",
        "Dec",
    ];
    const newDate = new Date(date);
    const getMonth = months[newDate.getMonth()];

    let time;
    if (newDate.getDate() === new Date().getDate()) {
        time = `${newDate.getHours()}:${newDate.getMinutes()}`;
    }
//What this does is if the current date matches the date entered it displays the time else it displays the date
    const fomattedDate = time
        ? ` Today at ${time}`
        : `${getMonth} ${newDate.getDate()}, ${newDate.getFullYear()}`;

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

&lt;/div&gt;



&lt;p&gt;Using the above function is simple. Import it into wherever you need to use it. In my case within a order map function in react. wrap the function around the date that requires formatting. &lt;code&gt;getDateHandler("2021-11-04T08:37:13.099+00:00")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below you can see a demo of how it works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import getDateHandler from "../utils/getDateHandler";

export default function UsingDateFormatScreen(props) {
    const createdAt="2021-11-04T08:37:13.099+00:00";
    return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Example Date : &amp;lt;br /&amp;gt; {getDateHandler(createdAt)} &amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Do let me know if you found this useful :)&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/0hwym"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Automagically creating a Sitemap for React, Node.js, MongoDB website</title>
      <dc:creator>TechSynthesis</dc:creator>
      <pubDate>Wed, 10 Nov 2021 11:13:44 +0000</pubDate>
      <link>https://forem.com/techsynthesis/automagically-creating-a-sitemap-for-react-nodejs-mongodb-website-3bpi</link>
      <guid>https://forem.com/techsynthesis/automagically-creating-a-sitemap-for-react-nodejs-mongodb-website-3bpi</guid>
      <description>&lt;p&gt;Sitemaps play an important role in SEO optimisation. Thus having a current sitemap is important. Automating a sitemap is easy. My sitemap generator runs on my node.js web server, automatically updating my sitemap.xml in my create-react-app website, public folder when I create or delete a page from site.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1. Create a new generate-sitemap.js file
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Step 2-A. Import the following
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { writeFileSync } from "fs";
import mongoose from "mongoose";

//Import the model which you want to autogenerate. 
import Product from "../models/productModel.js";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2-B. Connect to your DB
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mongodbUrl = config.MONGODB_URL;
mongoose
    .connect(mongodbUrl, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
    })
    .catch((error) =&amp;gt; console.log(error.reason));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2-C. Create the sitemap generator function
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const generateSitemap = async () =&amp;gt; {
    const products = await Product.find();
    const sitemap = ``;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2-D. Add the static pages
&lt;/h1&gt;

&lt;h3&gt;
  
  
  (non-changing urls i.e About, Home, Contact, Search, TOS, Privacy Policy, etc).
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const generateSitemap = async () =&amp;gt; {
    const products = await Product.find();
    const sitemap = `
    &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
    &amp;lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/about&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/contact&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/search&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/privacy&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/terms-of-use&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/shipping-and-returns&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
&amp;lt;/urlset&amp;gt;
`;
}

export default generateSitemap 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2-E. Add the dynamic pages after the last &lt;code&gt;&amp;lt;/url&amp;gt;&lt;/code&gt;
&lt;/h1&gt;

&lt;h3&gt;
  
  
  It can be products, categories, articles, etc.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  ${products
            .map((page) =&amp;gt; {
                return `
              &amp;lt;url&amp;gt;
                  &amp;lt;loc&amp;gt;${`https://www.yourWebsiteHere.com/product/${page.slug}`}&amp;lt;/loc&amp;gt;
              &amp;lt;/url&amp;gt;
            `;
            })
            .join("")}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2-F: Write the file to your desired location.
&lt;/h1&gt;

&lt;h3&gt;
  
  
  I'm saving it to my public folder.
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;writeFileSync("./frontend/public/sitemap.xml", sitemap);
console.log("genarated Sitemap successfully");
    return true;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 3. Recreate the sitemap every time an new product is added.
&lt;/h1&gt;

&lt;p&gt;I want the generateSitemap function to run every time I add a new product. So I'm calling it after my product is created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import generateSitemap from "../scripts/generate-sitemap.js";

productRouter.post(
    "/",
    isAuth,
    expressAsyncHandler(async (req, res) =&amp;gt; {
            const createdProduct = await product.save();
            //calling generateSitemap
            generateSitemap();
            res.send({
                message: "Product has been added successfully!",
                product: createdProduct,
            });

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

&lt;/div&gt;



&lt;p&gt;You can do something similar or create a cron job to recreate the sitemap at a set interval.&lt;/p&gt;

&lt;p&gt;I'm using node-cron for that. I've scheduled it to run every morning at 6am and I've set the timezone closest to mine.&lt;/p&gt;

&lt;p&gt;Install node-cron using &lt;code&gt;npm install --save node-cron&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import cron from "node-cron";
cron.schedule(
    "0 6 * * *",
    () =&amp;gt; {
        generateSitemap();
    },
    {
        scheduled: true,
        timezone: "Asia/Colombo",
    },
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Finally once your site map is generated you can access it by typing your websitename.com/sitemap.xml or if you're on localhost then localhost:port/sitemap.xml.
&lt;/h4&gt;

&lt;p&gt;Complete Code for generate-sitemap.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { writeFileSync } from "fs";
import mongoose from "mongoose";
import config from "../config.js";

import Product from "../models/productModel.js";

const mongodbUrl = config.MONGODB_URL;
mongoose
    .connect(mongodbUrl, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
    })
    .catch((error) =&amp;gt; console.log(error.reason));

const generateSitemap = async () =&amp;gt; {
    const products = await Product.find();
    const sitemap = `
    &amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
    &amp;lt;urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/about&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/contact&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/search&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/privacy&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/terms-of-use&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
    &amp;lt;url&amp;gt;
        &amp;lt;loc&amp;gt;https://www.yourWebsiteHere.com/rules/shipping-and-returns&amp;lt;/loc&amp;gt;
    &amp;lt;/url&amp;gt;
        ${products
            .map((page) =&amp;gt; {
                return `
              &amp;lt;url&amp;gt;
                  &amp;lt;loc&amp;gt;${`https://www.yourWebsiteHere.com/product/${page.slug}`}&amp;lt;/loc&amp;gt;
              &amp;lt;/url&amp;gt;
            `;
            })
            .join("")}
    &amp;lt;/urlset&amp;gt;
    `;

    writeFileSync("./frontend/public/sitemap.xml", sitemap);
    console.log("genarated Sitemap successfully");
    return;
};

export default generateSitemap;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>react</category>
      <category>node</category>
      <category>tutorial</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
