<?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: Arifa</title>
    <description>The latest articles on DEV Community by Arifa (@arifamujawar).</description>
    <link>https://dev.to/arifamujawar</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%2F440261%2Fcda82e9a-50b6-40b1-97e2-bb6e399c32f7.jpeg</url>
      <title>DEV Community: Arifa</title>
      <link>https://dev.to/arifamujawar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arifamujawar"/>
    <language>en</language>
    <item>
      <title>Easily upgrade or downgrade your Node.js version</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Sat, 13 Feb 2021 13:07:04 +0000</pubDate>
      <link>https://dev.to/arifamujawar/easily-upgrade-or-downgrade-your-node-js-version-505k</link>
      <guid>https://dev.to/arifamujawar/easily-upgrade-or-downgrade-your-node-js-version-505k</guid>
      <description>&lt;p&gt;Sometimes, when you are working on a project which uses a different version of Node.js than what's installed on your dev machine, you may want to change to the Node.js version required by the project.&lt;/p&gt;

&lt;p&gt;All you have to do is install a package called &lt;a href="https://www.npmjs.com/package/n"&gt;n&lt;/a&gt;, which caches node versions on your dev machine.&lt;/p&gt;

&lt;p&gt;Install n using npm&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 n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After executing the above command, you may receive a series of npm warnings followed by npm ERR! You can execute these three commands which are also mentioned &lt;a href="https://www.npmjs.com/package/n"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# make cache folder (if missing) and take ownership
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
# take ownership of node install destination folders
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can install any version of Node.js you want.&lt;br&gt;
Executing as &lt;code&gt;n &amp;lt;version&amp;gt;&lt;/code&gt; you can download and install a version of Node.js. If the &lt;code&gt;&amp;lt;version&amp;gt;&lt;/code&gt; has already been downloaded, n will install from its cache.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MB_spNka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rv886hepsgo3mdwyosyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MB_spNka--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/rv886hepsgo3mdwyosyl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To change to the latest version of node&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lRidaXL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ioei43feqy84d811pm5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lRidaXL8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ioei43feqy84d811pm5i.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also check the versions of node your &lt;i&gt;n&lt;/i&gt; package has, by simply executing the following command &lt;code&gt;n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0q4_Wwc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ilpmnuyll5lfh9i6fin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0q4_Wwc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ilpmnuyll5lfh9i6fin.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Executing the above command provides the versions cached.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i0epFmt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ardjg7a5s1kfncykmz6f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i0epFmt7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ardjg7a5s1kfncykmz6f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this post helps you switch between different Node.js versions. Please like this post and follow me for more such posts.&lt;/p&gt;

&lt;p&gt;References&lt;br&gt;
[1] &lt;a href="https://www.npmjs.com/package/n"&gt;https://www.npmjs.com/package/n&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
    </item>
    <item>
      <title>How to host a full-stack application on Heroku</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Sat, 12 Dec 2020 12:18:37 +0000</pubDate>
      <link>https://dev.to/arifamujawar/how-to-host-a-full-stack-application-on-heroku-nel</link>
      <guid>https://dev.to/arifamujawar/how-to-host-a-full-stack-application-on-heroku-nel</guid>
      <description>&lt;p&gt;If you have built a full-stack application (using React, Node.js) that is running successfully locally on your dev-machine, you would like to host it on the internet so that you can share it with your friends and family. In this post, I will guide you through simple steps on how you can do that.&lt;/p&gt;

&lt;p&gt;You can host your application as a single project on Heroku.&lt;/p&gt;

&lt;p&gt;In the front-end part of your project, change baseUrl (or the URL that you use to communicate with backend) to a relative Url like as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const baseUrl = "/api"; //run locally using http://localhost:4000/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have created your front-end application using create-react-app a build file of the project can be created with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's run this command from the root of the frontend project.&lt;/p&gt;

&lt;p&gt;This creates a directory called &lt;strong&gt;build&lt;/strong&gt; which contains the directory static. &lt;a href="https://en.wikipedia.org/wiki/Minification_(programming)"&gt;Minified&lt;/a&gt; version of our application's JavaScript code will be generated to the static directory.&lt;/p&gt;

&lt;p&gt;Let's move to the backend side of the application.&lt;/p&gt;

&lt;p&gt;Heroku uses its own PORT so change the definition of the port your application uses at the bottom of the index.js file like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PORT = process.env.PORT || 3001
app.listen(PORT, () =&amp;gt; {
  console.log(`Server running on port ${PORT}`)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, copy the build file from the front-end project to the backend project.&lt;/p&gt;

&lt;p&gt;To make express serve the front end(ie. static content like index.html and the JavaScript, etc.,), we need a built-in middleware from express called &lt;a href="http://expressjs.com/en/starter/static-files.html"&gt;static&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We need to add the following amidst the declarations of middlewares&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use(express.static('build'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whenever express gets an HTTP GET request it will first check if the build directory contains a file corresponding to the request's address. If a correct file is found, express will return it.&lt;/p&gt;

&lt;p&gt;In the backend part of your project,&lt;br&gt;
add a file called &lt;strong&gt;Procfile&lt;/strong&gt; to the project's root to tell Heroku how to start the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also write this in Procfile if index.js is the entry point of your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;web: node index.js 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure you have initialized git and added .gitignore, doing so will ensure that your node_modules and sensitive data of your is not over the internet.&lt;/p&gt;

&lt;p&gt;Now, you can create a Heroku project using Heroku CLI or UI provided by Heroku. We will use the Heroku CLI.&lt;br&gt;
To start with Heroku CLI you need to first login into Heroku&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Fill in the email and password.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku create link_of_the_name_that_you_want_to_host_on
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;commit your code to the repository&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git commit -m "message" 

$ git push origin master

$ git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also check the logs on Heroku using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ heroku logs -t 
//which prints the logs to Console whenever something happens on the server.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So your application is now hosted on the Heroku. If you wish you can also check if the application works locally as a full-stack application, all you need to do change the Url to &lt;code&gt;http://localhost:4000/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The problem I faced hosting such an application was that If I had to make any changes in the front-end I had to change the build file of the backend.&lt;/p&gt;

&lt;p&gt;I hope this helps you host your application with some ease.&lt;/p&gt;

&lt;p&gt;Don’t hesitate to clap if you considered this a worthwhile read!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using WordPress API with React - Parsing Special Characters</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Tue, 20 Oct 2020 10:05:44 +0000</pubDate>
      <link>https://dev.to/arifamujawar/using-wordpress-api-with-react-parsing-special-characters-9fg</link>
      <guid>https://dev.to/arifamujawar/using-wordpress-api-with-react-parsing-special-characters-9fg</guid>
      <description>&lt;p&gt;While I was working with the &lt;strong&gt;WordPress REST API&lt;/strong&gt;, I encountered that the response from the WordPress API contained few undesired things such as HTML tags and special characters such as &lt;code&gt;&amp;amp;#8217;&lt;/code&gt;. For example:&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;p&amp;gt;Hey there! A young and eager to learn native English speaker
here. I&amp;amp;#8217;m a Materials Science graduate from the University
of Cambridge and have a broad range of experiences in
engineering and education, from saving elephants in 
South Africa to manufacturing design in Cyprus. I&amp;amp;#8217;m happy
to code and develop my skills as an intern.&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I wanted to get rid of the &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags and replace the special characters with appropriate &lt;em&gt;human-readable&lt;/em&gt; characters.&lt;/p&gt;

&lt;p&gt;After spending some time on solving this problem, I found a couple of node packages which solve my problem. They are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;striptags&lt;/li&gt;
&lt;li&gt;he&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's first start with striptags. &lt;em&gt;Install&lt;/em&gt; it using:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;npm i striptags&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Here's an &lt;em&gt;example&lt;/em&gt; usage of striptags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import striptags from 'striptags';
var html = &amp;lt;p&amp;gt;Functional consultant specialized on financials
 modules of Oracle E-Business Suite.&amp;lt;/p&amp;gt;

striptags(html);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can actually do a lot of things with this package, for example, you can decide which tags you want to keep, and which tags you want to replace.&lt;/p&gt;

&lt;p&gt;So the next problem was that I was receiving some special characters like I’m, which was meant to be I'm. To solve this,  I made use of a decoder named as &lt;a href="https://github.com/mathiasbynens/he"&gt;he&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Installed the package using:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install he&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Then, I used the package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import he from 'he';

he.decode('Hey there! A young and eager to learn native English 
speaker here. I&amp;amp;#8217;m a Materials Science graduate from the
University of Cambridge and have a broad range of experiences 
in engineering and education, from saving elephants in South
Africa to manufacturing design in Cyprus. I&amp;amp;#8217;m happy to code
and develop my skills as an intern.');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  And the output was
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hey there! A young and eager to learn native English speaker here.
I'm a Materials Science graduate from the University of Cambridge
and have a broad range of experiences in engineering and education, 
from saving elephants in South Africa to manufacturing design
in Cyprus. I'm happy to code and develop my skills as an intern.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I share my learning experience in form of writing blogs, so I write anything new learned. &lt;em&gt;Don’t hesitate to clap if you considered this a worthwhile read!&lt;/em&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>react</category>
    </item>
    <item>
      <title>Create a more personalized Github Profile</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Fri, 18 Sep 2020 08:29:23 +0000</pubDate>
      <link>https://dev.to/arifamujawar/create-a-more-personalized-github-profile-5gfo</link>
      <guid>https://dev.to/arifamujawar/create-a-more-personalized-github-profile-5gfo</guid>
      <description>&lt;p&gt;Are you excited to make your &lt;a href="https://github.com/ArifaMujawar"&gt;GitHub profile&lt;/a&gt; look better than it is now?&lt;/p&gt;

&lt;p&gt;Here is an example of how I made mine look better presenting more about me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--reTLo5nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/29464058/93574073-9fd76b00-f9a0-11ea-90a8-f09e49e5588b.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--reTLo5nV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/29464058/93574073-9fd76b00-f9a0-11ea-90a8-f09e49e5588b.jpg" alt="github-repo-creation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let's get started.&lt;/p&gt;

&lt;p&gt;To create a more personalized GitHub README you need to start as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to your GitHub account&lt;/li&gt;
&lt;li&gt;Create a new repository with the same name as your user name as shown below&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L35q4AH2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/29464058/93573468-d5c81f80-f99f-11ea-984f-cccdae5d44d5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L35q4AH2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://user-images.githubusercontent.com/29464058/93573468-d5c81f80-f99f-11ea-984f-cccdae5d44d5.jpg" alt="github-repo-creation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make the repository public and initialize with a README.md &lt;/li&gt;
&lt;li&gt;Click the "Create repository" button&lt;/li&gt;
&lt;li&gt;Start by making changes in the README.md file through GitHub editor which is the easiest way to do. You can also do it offline based on your preference and push back the changes to the repo when you are done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few things that a README file should contain &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A greeting message&lt;/li&gt;
&lt;li&gt;Bio about yourself&lt;/li&gt;
&lt;li&gt;Social media profiles&lt;/li&gt;
&lt;li&gt;Highlights, what all you are working on right now

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/anuraghazra/github-readme-stats"&gt;Github Stats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://visitor-badge.glitch.me/"&gt;Profile visitor tracker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


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

&lt;p&gt;Try creating your Github Page and let me know in the comments the fun you had creating it.&lt;/p&gt;

&lt;p&gt;Shower your love!!!&lt;/p&gt;

</description>
      <category>github</category>
    </item>
    <item>
      <title>Add Google Analytics to a React-based Website</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Sat, 01 Aug 2020 15:30:08 +0000</pubDate>
      <link>https://dev.to/arifamujawar/add-google-analytics-to-a-react-based-website-528g</link>
      <guid>https://dev.to/arifamujawar/add-google-analytics-to-a-react-based-website-528g</guid>
      <description>&lt;p&gt;Google provides a &lt;em&gt;free&lt;/em&gt; web analytics service that can be used to analyze details about the visitors on our website.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tracking ID&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly, we need to have a Google Analytics Account, Go to &lt;a href="http://www.google.com/analytics/"&gt;Google Analytics&lt;/a&gt;. Once you have signed in to your Google account, click Access Google Analytics, and click Sign up.&lt;/li&gt;
&lt;li&gt; Fill in your account name, website name, website URL, and select an &lt;em&gt;Industry Category&lt;/em&gt; and also the &lt;em&gt;Reporting Time Zone&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;Under &lt;em&gt;Data Sharing Options&lt;/em&gt;, check the boxes next to the options that you want. &lt;/li&gt;
&lt;li&gt;Click &lt;em&gt;Get Tracking ID&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Configuring the React Project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the project where you want to add google analytics follow these steps:&lt;/p&gt;

&lt;p&gt;Install &lt;em&gt;react-ga&lt;/em&gt; package&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 react-ga --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initializing GA and Tracking Pageviews:&lt;br&gt;
Add the following snippet of code in your Index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactGA from 'react-ga';
ReactGA.initialize('UA-tracking-id'); // add your tracking id here.
ReactGA.pageview(window.location.pathname + window.location.search);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy the changes.&lt;br&gt;
Now you can view the analytics in &lt;a href="http://www.google.com/analytics/"&gt;Google Analytics&lt;/a&gt; account.&lt;/p&gt;

</description>
      <category>react</category>
      <category>analytics</category>
    </item>
    <item>
      <title>Using morgan with Express for Backend Logging</title>
      <dc:creator>Arifa</dc:creator>
      <pubDate>Sat, 25 Jul 2020 09:53:39 +0000</pubDate>
      <link>https://dev.to/arifamujawar/using-morgan-with-express-for-backend-logging-30g7</link>
      <guid>https://dev.to/arifamujawar/using-morgan-with-express-for-backend-logging-30g7</guid>
      <description>&lt;p&gt;&lt;em&gt;Logging&lt;/em&gt; is an important element of writing software as it helps us to see the software operations, debug and catch errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;morgan&lt;/strong&gt; is a logging tool (middleware) that can be used in HTTP servers implemented using Express &amp;amp; Node.js. It can be used to log requests, errors, and more to the console.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Middleware are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next [1].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Getting started with morgan
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install morgan --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;em&gt;basic code&lt;/em&gt; snippet using morgan looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const morgan = require('morgan');

const app = express();

app.listen(3000, () =&amp;gt; {
     console.log('App listening on port 3000 ...');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('App listening on port 3000 ...');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Formatting Logs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Making use of predefined format strings
&lt;/h3&gt;

&lt;p&gt;morgan offers us with a set of predefined format strings, which are plug-and-play. For example, let's look at &lt;code&gt;morgan('tiny')&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;const express = require('express');
const morgan = require('morgan');

const app = express();
app.use(morgan('tiny'));

app.get("/", (req, res) =&amp;gt; {
  res.send("&amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;");
});

app.listen(3000, () =&amp;gt; {
    console.log('App listening on port 3000 ...');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open your web browser and go to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt;. You will see the following output in your web server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App listening on port 3000 ...
GET / 200 24 - 2.868 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is displayed in the following format:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;morgan(':method :url :status :res[content-length] - :response-time ms')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can find more such predefined formats from &lt;a href="https://github.com/expressjs/morgan#predefined-formats"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom Log Formats by Creating Our Own Tokens
&lt;/h3&gt;

&lt;p&gt;We can create our own custom log format strings. Below is an example of how we can create our own custom log format strings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const morgan = require('morgan');

const app = express();

//we are defining a new parameter called host
morgan.token('host', function(req, res) {
    return req.hostname;
});

// we are using the host parameter
app.use(morgan(':method :host :status :res[content-length] - :response-time ms'))

app.get("/", (req, res) =&amp;gt; {
  res.send("&amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;");
});

app.listen(3000, () =&amp;gt; {
    console.log('App listening on port 3000 ...');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App listening on port 3000 ...
GET localhost 200 21 - 2.819 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this post has helped you to get started with logging using morgan. &lt;em&gt;Please like this post and follow me for more such posts.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;br&gt;
[1]  &lt;a href="http://expressjs.com/en/guide/using-middleware.html#using-middleware"&gt;http://expressjs.com/en/guide/using-middleware.html#using-middleware&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>backend</category>
      <category>logging</category>
      <category>express</category>
    </item>
  </channel>
</rss>
