<?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: Utkarsh Singh</title>
    <description>The latest articles on Forem by Utkarsh Singh (@singhutkarshh).</description>
    <link>https://forem.com/singhutkarshh</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%2F592634%2F178b173b-e312-4365-9b5f-d518fbfb2994.jpg</url>
      <title>Forem: Utkarsh Singh</title>
      <link>https://forem.com/singhutkarshh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/singhutkarshh"/>
    <language>en</language>
    <item>
      <title>Basic server side caching using Redis in nodejs</title>
      <dc:creator>Utkarsh Singh</dc:creator>
      <pubDate>Tue, 23 Nov 2021 11:20:21 +0000</pubDate>
      <link>https://forem.com/singhutkarshh/basic-server-side-caching-using-redis-4e1f</link>
      <guid>https://forem.com/singhutkarshh/basic-server-side-caching-using-redis-4e1f</guid>
      <description>&lt;p&gt;Caching is the process of storing copies of files in a cache, or temporary storage location, so that they can be accessed more quickly. &lt;br&gt;
Caching helps us in making our website more faster, respond to user queries faster by acting as a middleware between server and database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodeahoy.com%2Fimg%2Fcache-aside.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcodeahoy.com%2Fimg%2Fcache-aside.png" alt="Caching process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;There is commonly two types of caching :-&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) Server side caches are generally used to avoid making expensive database operations repeatedly to serve up the same content to lots of different clients. &lt;/p&gt;

&lt;p&gt;2) Client side caches are used to avoid transferring the same data over the network repeatedly.&lt;/p&gt;

&lt;p&gt;Today we will learn basic server side caching using redis(a fast, open source, in-memory, key-value data store).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Installing Redis:-&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly we will need to install redis before using it in our project.&lt;/p&gt;

&lt;p&gt;Installing redis on Mac using Homebrew -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install redis
brew services start redis
redis-server /usr/local/etc/redis.conf

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Installing redis on Windows -&lt;/em&gt;&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;Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install redis-server
sudo service redis-server restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Installing redis on ubuntu -&lt;/em&gt;&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;sudo apt update
sudo apt install redis-server
sudo nano /etc/redis/redis.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the file, find the supervised directive. This directive allows you to declare an init system to manage Redis as a service, providing you with more control over its operation. The supervised directive is set to no by default. Since you are running Ubuntu, which uses the systemd init system, change this to systemd:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;. . .

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised systemd

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

&lt;/div&gt;



&lt;p&gt;and finally restart redis&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart redis.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;1)&lt;/em&gt;&lt;/strong&gt; In the project folder initialise the project using npm init and install express, redis and node-fetch(same as fetch in javascript for making requests to rest clients ). Also install nodemon as dev dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm init -y
npm install --save express redis
npm install --dev nodemon 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;2)&lt;/em&gt;&lt;/strong&gt; In the project folder paste this code in app.js importing express , nodefetch and redis and start basic server.&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 app = express();
const redis = require("redis");
const fetch = require("node-fetch");

app.get("/", (req, res) =&amp;gt; {
    res.status(200).send("This is homepage!");
})

app.listen(8080, () =&amp;gt; {
     console.log("Server started!");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;3)&lt;/em&gt;&lt;/strong&gt; Create a redis client passing default redis port(6379) as parameter and also create a new route(/post) that will fetch data from &lt;a href="https://api.github.com/users" rel="noopener noreferrer"&gt;https://api.github.com/users&lt;/a&gt; and send data as response.&lt;br&gt;
We will cache this data on our first visit to server and after that in all visits we will check if data is stored in cache or not . If it is stored we will not fetch it instead send response from cache.&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 app = express();
const redis = require("redis");
const fetch = require("node-fetch");

const client = redis.createClient(6379);

app.get("/posts", (req, res) =&amp;gt; {
    console.log("fetching data")    // this will tell uswe are fetching data  from api
    fetch(`https://api.github.com/users`,((response)=&amp;gt;{
       const data = response.json();
       client.set("userData",data);   // save data(key,value pair) in redis in form of cache

     res.send({data:data});
   })
})

app.listen(8080, () =&amp;gt; {
     console.log("Server started!");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we used client.set(key,value) for saving data in redis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;4)&lt;/em&gt;&lt;/strong&gt; We will now create a middleware and add it in "/post" route for checking if cache already exists.If data is already present in cache we will return it directly else we will leave our middleware and fetch it from the route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const checkDataInCache = (req,res,next) =&amp;gt;{
  const  data = client.get("userData"); //get data from cache and check if it exists
  if(data !== null){
    res.send({data:data});
  }else{
    next();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are almost done with our code(full code given at last) and now we will test it.&lt;/p&gt;

&lt;p&gt;If we send a get request at "/posts" at first we will see log as " fetching .." that  shows that we are fetching data from api.&lt;br&gt;
But after that in all requests there will be no log and data will be loaded more quickly.&lt;br&gt;
(We can check for the speed by going in console and them network).&lt;/p&gt;

&lt;p&gt;This was basic representation of how to use caching.Full code given below.&lt;br&gt;
Hope it helps!!&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 app = express();
const redis = require("redis");
const fetch = require("node-fetch");

const client = redis.createClient(6379);

app.get("/posts",checkDataInCache, (req, res) =&amp;gt; {
    console.log("fetching data")    // this will tell us if we are fetching data  from api
    fetch(`https://api.github.com/users`,((response)=&amp;gt;{
       const data = response.json();
       client.set("userData",data);   // save data(key,value pair) in redis in form of cache

     res.send({data:data});
   })
})

const checkDataInCache = (req,res,next) =&amp;gt;{
  const  data = client.get("userData"); //get data from cache and check if it exists
  if(data !== null){
    res.send({data:data});
  }else{
    next();
  }
}
app.listen(8080, () =&amp;gt; {
     console.log("Server started!");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>node</category>
      <category>redis</category>
      <category>caching</category>
    </item>
    <item>
      <title>Token based authentication in nodejs</title>
      <dc:creator>Utkarsh Singh</dc:creator>
      <pubDate>Sun, 21 Nov 2021 15:38:27 +0000</pubDate>
      <link>https://forem.com/singhutkarshh/token-based-authentication-in-nodejs-4amm</link>
      <guid>https://forem.com/singhutkarshh/token-based-authentication-in-nodejs-4amm</guid>
      <description>&lt;p&gt;There are times when we need to authenticate the user before giving him access to specific pages on our website.This authentication ensures that user has access to only those data that he has privileges on.&lt;/p&gt;

&lt;p&gt;An entry level programmer would just fetch username and password stored in database at the time of login and if they match would give him access , which is not wrong but only half  a step in the process of authentication.&lt;/p&gt;

&lt;p&gt;It is also necessary to ensure that of all the data stored in database only data related to user is shown.&lt;/p&gt;

&lt;p&gt;This can be achieved in two ways:&lt;br&gt;
1- Token based authentication(using jwt-jsonWebToken)&lt;br&gt;
2- Session based authentication&lt;/p&gt;

&lt;p&gt;Today we will talk and implement token based authentication in NodeJs.&lt;/p&gt;

&lt;p&gt;1- Install following packages and dependencies which we are going to work with - &lt;br&gt;
we will build our server with express , jsonWebToken is library used for creating and verifying tokens and dotenv for storing our secrets in .env file that will not be visible to others.&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 --save express jsonWebToken dotenv
npm install -D nodemon
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2- .env file contains two things:&lt;br&gt;
1-SECRET_ACCESS_TOKEN&lt;br&gt;
2-REFRESH_TOKEN&lt;/p&gt;

&lt;p&gt;Secret access token is a secret code that  we use to verify ourself as creator of tokens and same secret is used while verifying tokens too.&lt;/p&gt;

&lt;p&gt;Refresh tokens are used to create new access token once they expire.&lt;br&gt;
(We will not be implemeting refresh tokens for now)&lt;br&gt;
e.g. -&lt;br&gt;
These tokens can be created randomly using encrypt library in nodejs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SECRET_ACCESS_TOKEN="9c2fa79645d6210a31e1cfb8435f06d5c10d9c7e5e80069e91a52fc870b05409"
SECRET_REFRESH_TOKEN="f1f0e9c17f296226431f4468ed329781b3b774583c86462247576c2d92f01900"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3-Create a basic server in app.js file containing  following code and start the server with nodemon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require("dotenv").config();
const express = require("express");
const app = express();
const jwt = require("jsonwebtoken");

app.get("/", (req, res) =&amp;gt; {
    res.status(200).send("This is homepage!");
})

app.listen(process.env.PORT, () =&amp;gt; {
     console.log("Server started!");
});

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

&lt;/div&gt;



&lt;p&gt;4- Now we will create a new route("/login) that  will check for user id and password at the time of login and generate token for the same user that  we will pass in headers with every request we make ever after.&lt;br&gt;
After authentication is successful we go ahead and create a token using jwt.sign(user,token) , it signs the token with the user we enter and will return the same user when we will verify the token.&lt;br&gt;
If authentication fails , we tell user to enter correct credentials.&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 app = express();
const jwt = require("jsonwebtoken");
require("dotenv").config();

app.get("/", (req, res) =&amp;gt; {
    res.status(200).send("This is homepage!");
})

app.post("/login", (req, res) =&amp;gt; {
    const {username , password} = req.body; //get username and password that we passed client side.

    //Authenticate with username and password stored in database.Do it yourself!

   if(Authentication is successfull)
   {

   //Create a token signed by username

      const user = {name : req.body.username}
      const accessToken = jwt.sign(user , process.env.SECRET_ACCESS_TOKEN);
      res.send({accessToken : accessToken});
   }
   else
    {
      res.send("Wrong Credentials!");
    }
})

app.listen(process.env.PORT, () =&amp;gt; {
     console.log("Server started!");
});

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

&lt;/div&gt;



&lt;p&gt;5- Now we have created a token and sent it to client side , this token will be passed in headers  with every request  to authenticate for the user and show him data related to user  only.&lt;br&gt;
For verifying we will create a middleware(autenticateToken).It takes access token from the headers that is passed client side and verifies it using jwt.verify(token , secret-access-token , (error,user)=&amp;gt;{})&lt;br&gt;
.The callback returns user info  that  is saved in res so that it is accessible in our route;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   if(Authentication is successfull)
   {

   //Create a token signed by username

      const user = {name : req.body.username}
      const accessToken = jwt.sign(user , process.env.SECRET_ACCESS_TOKEN);
      res.send({accessToken : accessToken});
   }
   else
    {
      res.send("Wrong Credentials!");
    }
})

const authenticateToken = (req,res,next) =&amp;gt;{

  // We will pass token in the following format =&amp;gt; "token"

  const accessToken = req.headers['authorization'];

  if (accessToken == null)
  return res.sendStatus(401);

  jwt.verify(accessToken , process.env.SECRET_ACCESS_TOKEN,(err,data)=&amp;gt;{
    if (err) return res.status(402).send(err);
    req.user = data;
    next();
  })
}

app.listen(process.env.PORT, () =&amp;gt; {
     console.log("Server started!");
});

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

&lt;/div&gt;



&lt;p&gt;6 -It verifies the token and in callback return error and the user info that we can use to filter out contents from our database , since here we are not connected to a database we will create an array of posts to check if token works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const posts = [{
  {username : "Bob" , title:"superman" , serial : 1},
  {username : "Allen" , title:"Batman" , serial : 2},
  {username : "Ray" , title:"Iron Man" , serial : 3}
 }];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7 - We create a new route("/posts") to test our tokens and add this middleware in our "/posts" route and then filter our content out with our username.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const posts = [{
  {username : "Bob" , title:"superman" , serial : 1},
  {username : "Allen" , title:"Batman" , serial : 2},
  {username : "Ray" , title:"Iron Man" , serial : 3}
 }];

app.get("/posts", authenticateToken , (req,res)=&amp;gt;{
    res.json(posts.filter((post)=&amp;gt;  post.username == req.user.name));
});


const authenticateToken = (req,res,next) =&amp;gt;{

  // We will pass token in the following format =&amp;gt; "token"

  const accessToken = req.headers['authorization'];

  if (accessToken == null)
  return res.sendStatus(401);

  jwt.verify(accessToken , process.env.SECRET_ACCESS_TOKEN,(err,data)=&amp;gt;{
    if (err) return res.status(402).send(err);
    req.user = data;
    next();
  })
}

app.listen(process.env.PORT, () =&amp;gt; {
     console.log("Server started!");
});

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

&lt;/div&gt;



&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;if we passed username as Bob we get :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{username : "Bob" , title:"superman" , serial : 1}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how we authenticate using tokens and filter out data of our user.&lt;br&gt;
This token can also be set for automatic expiry of 1 min(or as we like) by passing in  an expiry time jwt.sign(user,SECRET_ACCESS_TOKEN , 3600).&lt;/p&gt;

&lt;p&gt;Complete  Code :&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 app = express();
const jwt = require("jsonwebtoken");
require("dotenv").config();

 const posts = [{
  {username : "Bob" , title:"superman" , serial : 1},
  {username : "Allen" , title:"Batman" , serial : 2},
  {username : "Ray" , title:"Iron Man" , serial : 3}
 }];

app.get("/posts", authenticateToken , (req,res)=&amp;gt;{
    res.json(posts.filter((post)=&amp;gt;  post.username == req.user.name));
});

app.post("/login", (req, res) =&amp;gt; {
    const {username , password} = req.body; //get username and password that we passed client side.

  //Authenticate with username and password stored in database.Do it yourself!

   if(Authentication is successfull)
   {

   //Create a token signed by username

      const user = {name : req.body.username}
      const accessToken = jwt.sign(user , process.env.SECRET_ACCESS_TOKEN);
      res.send({accessToken : accessToken});
   }
   else
    {
      res.send("Wrong Credentials!");
    }
})

app.get("/", (req, res) =&amp;gt; {
    res.status(200).send("This is homepage!");
})

const authenticateToken = (req,res,next) =&amp;gt;{

  // We will pass token in the following format =&amp;gt; "token"

  const accessToken = req.headers['authorization'];

  if (accessToken == null)
  return res.sendStatus(401);

  jwt.verify(accessToken , process.env.SECRET_ACCESS_TOKEN,(err,data)=&amp;gt;{
    if (err) return res.status(402).send(err);
    req.user = data;
    next();
  })
}



app.listen(process.env.PORT, () =&amp;gt; {
     console.log("Server started!");
});

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

&lt;/div&gt;



&lt;p&gt;**Important - &lt;/p&gt;

&lt;p&gt;We usually create an access token and refresh token seperately.Access token have an expiry that are refreshed by refresh token by creating a seperate function.**&lt;/p&gt;

&lt;p&gt;Hope it helps!&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
