<?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: Adeyemi Ibrahim</title>
    <description>The latest articles on DEV Community by Adeyemi Ibrahim (@slowlybutsteadylearning).</description>
    <link>https://dev.to/slowlybutsteadylearning</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%2F1019085%2Ff9635ca5-6678-44ed-9b0b-5b27b563a6aa.png</url>
      <title>DEV Community: Adeyemi Ibrahim</title>
      <link>https://dev.to/slowlybutsteadylearning</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slowlybutsteadylearning"/>
    <language>en</language>
    <item>
      <title>Authorization in Node.js, all you need to know</title>
      <dc:creator>Adeyemi Ibrahim</dc:creator>
      <pubDate>Tue, 21 Mar 2023 13:39:08 +0000</pubDate>
      <link>https://dev.to/slowlybutsteadylearning/authorization-in-nodejs-all-you-need-to-know-1bib</link>
      <guid>https://dev.to/slowlybutsteadylearning/authorization-in-nodejs-all-you-need-to-know-1bib</guid>
      <description>&lt;h1&gt;
  
  
  What is authentication &amp;amp; authorization
&lt;/h1&gt;

&lt;p&gt;Authentication and authorization are used in security, particularly when it comes to getting access to a system. Yet, there is a significant distinction between gaining entry into a house (authentication) and what you can do while inside (authorization).&lt;/p&gt;

&lt;p&gt;In simple words, Authentication is the process of verifying who a user is (who you are), and  Authorization is the process of verifying what they have access to (what you are allowed to do).&lt;/p&gt;

&lt;p&gt;Authorization is the process of allowing authenticated users access to resources by determining whether they have system access permissions. By giving or denying specific licenses to an authenticated user, authorization enables you to control access privileges.&lt;br&gt;
So, authorization occurs after the system authenticates your identity, granting you complete access to resources such as information, files, databases, funds, places, and anything else. That said, authorization affects your capacity to access the system and the extent to which you can do so&lt;/p&gt;

&lt;p&gt;Authorization is a critical component of any web application, as it controls access to various resources based on user roles and permissions. Node.js is a popular platform for building web applications, and there are various approaches to implement authorization in Node.js applications. Here is all you need to know about authorization in Node.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Authorization
&lt;/h2&gt;

&lt;p&gt;There are generally two types of authorization:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Role-based Authorization: In this type of authorization, access is granted or denied based on the user's role or job function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Permission-based Authorization: In this type of authorization, access is granted or denied based on the specific permissions granted to the user.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Implementing Authorization in Node.js
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const authorize = (req, res, next) =&amp;gt; {
  if (!req.user || !req.user.permissions.includes('view_data')) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  next();
};

const getData = (req, res) =&amp;gt; {
  const data = { /* data */ };
  res.json(data);
};

// /data route that requires authorization
app.get('/data', authorize, getData);

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

&lt;/div&gt;



&lt;p&gt;This code defines a middleware function called authorize that checks if the user is authenticated and has the required permissions to access the resource. If not, the middleware returns a 401 Unauthorized error. It then defines a route handler function called getData that retrieves data from a data source and returns it to the client.&lt;/p&gt;

&lt;p&gt;Finally, it uses the authorize middleware function to restrict access to the getData route handler function. This means that only authenticated users with the required permissions can access the /data route and retrieve data. In conclusion, this example demonstrates how to implement authorization using middleware functions in Node.js to ensure that users have the appropriate access to specific resources or functionalities based on their identity and permissions.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to create Nodejs server without an external package</title>
      <dc:creator>Adeyemi Ibrahim</dc:creator>
      <pubDate>Mon, 13 Feb 2023 16:09:42 +0000</pubDate>
      <link>https://dev.to/slowlybutsteadylearning/how-to-create-nodejs-server-without-an-external-package-456p</link>
      <guid>https://dev.to/slowlybutsteadylearning/how-to-create-nodejs-server-without-an-external-package-456p</guid>
      <description>&lt;p&gt;In this post we will just move into Nodejs fundamental by creating a Nodejs server without the use of any external packages like Express, Mongoose or other third party libraries to implement our functionality.&lt;/p&gt;

&lt;p&gt;We start by building a raw HTTP Nodejs with no framework , no npm  and no package.json but with the use of a &lt;strong&gt;terminal&lt;/strong&gt;(Gitbash or any terminal of your choice) and an &lt;strong&gt;editor&lt;/strong&gt;(VScode or any editor of your choice) by using Javascript and nodejs as our runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTTP :
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;http&lt;/code&gt; in Nodejs is an inbuilt module that provide  an interface to create HTTP(clients or server) that communicate with other HTTP(clients or server) via HTTP protocols.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therfore we will build a server that will&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import module&lt;/li&gt;
&lt;li&gt;create a server instance &lt;/li&gt;
&lt;li&gt;listen to the server&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a HTTP server
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;First, we need set up an accessible coding enviroment. in the terminal create a folder called &lt;code&gt;01-Server&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $ mkdir 01-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Then, open/enter the folder&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd 01-server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now, create a js file that you will type the code.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Then, open the file&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ nano httpserver.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Import Module
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;There are many inbuit module in Nodejs apart from 'http' like 'os', 'fs', 'path', and many more but our main focus is on 'http'.&lt;br&gt;
So in other for we to use 'http' we must require the 'http' module.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      const http = require("http");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating a Server Instance
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Creating a server from the imported module then callback/invoke the function.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       const server = http.createServer();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;createServer&lt;/code&gt; has another argument it takes in a call back function which has to run everytime a request comes in to our server. Inside the callback function, we have access to two different type of object: Request&lt;code&gt;req&lt;/code&gt; and Response&lt;code&gt;res&lt;/code&gt; object. The request object is full of information about the &lt;code&gt;url&lt;/code&gt; requested. Other information on the request type are &lt;strong&gt;GET, PUT, POST, DELETE&lt;/strong&gt;. While the response&lt;code&gt;res&lt;/code&gt; object is the object we use to send a object to the user&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      const server = http.createServer((req,res)=&amp;gt;{
             console.log("request made to the server");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Listen to Server Request
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;For the server to listen to the request, we have to define two constants, the host and the port which will be pass in as an arguement into our server.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     const host = "localhost";
     const port = 5757;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The value of the local host is &lt;code&gt;127.0.0.1&lt;/code&gt; and its only available to all local computer while the port numbers are like a door into our computer. Therefore, when we bind our server to this host and port, we'll be able to reach our server on &lt;code&gt;https://localhost:5757&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    server.listen(port, host,()=&amp;gt; {
       console.log("server ✌✌✌ is running on a port 5757");
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to run Nodejs on our terminal
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Press &lt;code&gt;Ctrl J&lt;/code&gt; if you are using VScode to open the terminal&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USER@DESKTOP-IK7TQLK MINGW64 ~/Desktop/01-Server
$

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Then, collecting all our codes together we have&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

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

   const server = http.createServer((req,res)=&amp;gt;{
         console.log("request made to the server");
   });

   const host = "localhost";
   const port = 5757;

   server.listen(port, host,()=&amp;gt; {
          console.log("server ✌✌✌ is running on a port 5757")
   });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now, we run our code on the terminal using &lt;code&gt;node httpserver.js&lt;/code&gt; or &lt;code&gt;node httpserver&lt;/code&gt; where &lt;code&gt;httpserver.js&lt;/code&gt; is the name of our &lt;code&gt;js&lt;/code&gt; file created&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USER@DESKTOP-IK7TQLK MINGW64 ~/Desktop/01-Server
$ node httpserver.js

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USER@DESKTOP-IK7TQLK MINGW64 ~/Desktop/01-Server
$ node httpserver.js
server ✌✌✌ is running on a port 5757
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now, we see &lt;code&gt;server ✌✌✌ is running on a port 5757&lt;/code&gt; which is in our &lt;code&gt;console.log("server ✌✌✌ is running on a port 5757")&lt;/code&gt;. This shows that this server is actively running and listening to request on &lt;code&gt;localhost:5757&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, we go to the browser to send a request using &lt;code&gt;localhost:5757&lt;/code&gt; or &lt;code&gt;127.0.0.1:5757&lt;/code&gt;. Our browser keep loading without any response but checking our terminal which shows &lt;code&gt;request made to the server&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USER@DESKTOP-IK7TQLK MINGW64 ~/Desktop/01-Server
$ node httpserver.js
server ✌✌✌ is running on a port 5757
request made to the server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From our server instance, whenever a request is being made to the server print &lt;code&gt;request made to the server&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 server = http.createServer((req,res)=&amp;gt;{
             console.log("request made to the server");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we've have successfully create a server with Nodejs &lt;/p&gt;

&lt;h2&gt;
  
  
  REQUEST AND RESPONSE
&lt;/h2&gt;

&lt;p&gt;Now, lets create a server using that will log a request and send back a response&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const http = require("http");
  const server = http.createServer((req,res)=&amp;gt;{
     console.log("Request made");

   if (req.url=="/") {
    res.writeHead(200, "Content-Type", "text/html");
    res.write("&amp;lt;h1&amp;gt;Welcome&amp;lt;/h1&amp;gt;");
    res.end();
} else if (req.url == "/login") {
    res.writeHead(201, "Content-Type", "text/html");
    res.write("&amp;lt;h1&amp;gt;LOGIN SUCCESSFULLY&amp;lt;/h1&amp;gt;");
    res.end();
} else {
    res.writeHead(404, "Content-Type","text/plain");
    res.write("PAGE NOT FOUND");
    res.end();
}
});
 const host = "localhost";
 const port = 5757;

 server.listen(port, host,(req, res) =&amp;gt;{
  console.log("server is up and running 🏃‍♂️🏃‍♂️🏃‍♂️");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>crypto</category>
      <category>web3</category>
      <category>offers</category>
    </item>
  </channel>
</rss>
