<?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: Mahmudul Hasan</title>
    <description>The latest articles on DEV Community by Mahmudul Hasan (@hasanmahmudul).</description>
    <link>https://dev.to/hasanmahmudul</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%2F778337%2Ffda9cc50-644c-459e-bc96-cc96d90cf61b.png</url>
      <title>DEV Community: Mahmudul Hasan</title>
      <link>https://dev.to/hasanmahmudul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hasanmahmudul"/>
    <language>en</language>
    <item>
      <title>#JWT #ExpressJS #Mongoose</title>
      <dc:creator>Mahmudul Hasan</dc:creator>
      <pubDate>Fri, 24 Dec 2021 16:07:15 +0000</pubDate>
      <link>https://dev.to/hasanmahmudul/jwt-expressjs-mongoose-54nk</link>
      <guid>https://dev.to/hasanmahmudul/jwt-expressjs-mongoose-54nk</guid>
      <description>&lt;h1&gt;
  
  
  JWT ( JSON Web Token )
&lt;/h1&gt;

&lt;p&gt;JSON Web Token (JWT) is a standard that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. The compact size makes the tokens easy to transfer through an URL, POST parameter, or inside an HTTP header. The information in a JWT is digitally signed using a secret or public/private key pair.&lt;br&gt;
JWTs can be signed using a secret or a public/private key pair.&lt;br&gt;
JWTs are mainly used for authentication. After a user signs in to an application, the application then assigns JWT to that user. Subsequent requests by the user will include the assigned JWT. This token tells the server what routes, services, and resources the user is allowed to access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of Node.js authentication with JWT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Node.js authentication with JWT has several advantages over the traditional authentication process, primarily the scalability of stateless applications. And since it’s becoming popular among such heavyweights as Facebook and Google, it’s adoption across the industry likely will continue to grow. &lt;/p&gt;

&lt;p&gt;Other advantages include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple verification through a JSON Web Token &lt;/li&gt;
&lt;li&gt;You can use an authentication service or outsource it&lt;/li&gt;
&lt;li&gt;Provides more trustworthiness than cookies or sessions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Need for JSON Web Token&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are several reasons why applications use JSON Web Tokens for authentication:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JWT is an excellent choice to be passed in HTML and HTTP environments due to its smaller footprint when compared to other types of tokens.&lt;/li&gt;
&lt;li&gt;JSON Web Tokens can be signed using a shared secret and also by using public/private key pairs.&lt;/li&gt;
&lt;li&gt;It is easier to work with JWT as JSON parsers are standard in most programming languages.&lt;/li&gt;
&lt;li&gt;JWT is also suitable for implementing authorization in large-scale web applications.&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  ExpressJs
&lt;/h1&gt;

&lt;p&gt;Express is a flexible Node.js web application framework that provides a wide set of features to develop both web and mobile applications.&lt;/p&gt;

&lt;p&gt;Express.js makes it much easier and simpler to build a web server with the use of middleware, which can handle requests and responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features of Express.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The main features of Express.js include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The capability to design single-page, multi-page, and hybrid web applications.&lt;/li&gt;
&lt;li&gt;Options for setting up middleware to respond to HTTP requests.&lt;/li&gt;
&lt;li&gt;It defines a routing table that is used to perform different actions based on the HTTP method and URL.&lt;/li&gt;
&lt;li&gt;Enables users to dynamically render HTML pages based on passing arguments to templates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A basic “Hello Express” example in node.js express&lt;/p&gt;

&lt;p&gt;open terminal and create folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir start-express
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;inside the folder 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 init -y
&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;npm install express -save
&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;create file index.js and inside code here 
const express = require ("epress");
const app = express( );
const port = process.env.port || 3000;

app.get('/',(req,res)=&amp;gt;{
re.send("Hello Express")
});

app.listen (port,( )=&amp;gt;{
 console.log (“listening to port”,port);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The request object represents the HTTP request and contains properties for the request query string, parameters, body, HTTP headers, and so on.&lt;br&gt;
The response object represents the HTTP response that an Express app sends when it receives an HTTP request.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Process : *&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implementing a callback function with parameters ‘request’ and ‘response’&lt;/li&gt;
&lt;li&gt;The application will listen on the defined port, which in this case is “3000,” and variables ‘port’ will contain the address and the port respectively&lt;/li&gt;
&lt;li&gt;Lastly, the console.log statement shows the address and port in the command prompt or terminal&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Displaying output in browser :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Hello Express&lt;/code&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Mongoose
&lt;/h1&gt;

&lt;p&gt;Mongoose is an Object Document Mapper (ODM). This means that Mongoose allows you to define objects with a strongly-typed schema that is mapped to a MongoDB document.&lt;/p&gt;

&lt;p&gt;Mongoose provides an incredible amount of functionality around creating and working with schemas. Mongoose currently contains eight SchemaTypes that a property is saved as when it is persisted to MongoDB. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;li&gt;Buffer&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Mixed&lt;/li&gt;
&lt;li&gt;ObjectId&lt;/li&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each data type allows you to specify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a default value.&lt;/li&gt;
&lt;li&gt;a custom validation function.&lt;/li&gt;
&lt;li&gt;indicate a field is required.&lt;/li&gt;
&lt;li&gt;a get function that allows you to manipulate the data before it is returned as an object.&lt;/li&gt;
&lt;li&gt;a set function that allows you to manipulate the data before it is saved to the database.&lt;/li&gt;
&lt;li&gt;create indexes to allow data to be fetched faster.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Further to these common options, certain data types allow you to further customize how the data is stored and retrieved from the database. For example, a String data type also allows you to specify the following additional options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;convert it to lowercase.&lt;/li&gt;
&lt;li&gt;convert it to uppercase.&lt;/li&gt;
&lt;li&gt;trim data prior to saving.&lt;/li&gt;
&lt;li&gt;a regular expression that can limit data allowed to be saved during the validation process.&lt;/li&gt;
&lt;li&gt;an enum that can define a list of strings that are valid.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>jwt</category>
      <category>express</category>
      <category>mongoose</category>
    </item>
    <item>
      <title>#JSX #Component lifecycle #Context Api</title>
      <dc:creator>Mahmudul Hasan</dc:creator>
      <pubDate>Thu, 23 Dec 2021 17:37:23 +0000</pubDate>
      <link>https://dev.to/hasanmahmudul/jsx-component-lifecycle-context-api-4gd7</link>
      <guid>https://dev.to/hasanmahmudul/jsx-component-lifecycle-context-api-4gd7</guid>
      <description>&lt;p&gt;&lt;strong&gt;# JSX :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JSX is a JavaScript Extension Syntax used in React to easily write HTML and JavaScript together.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const jsx = &amp;lt;h1&amp;gt;this is jsx &amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is simple JSX code in React. But the browser does not understand this JSX because it's not valid JavaScript code. This is because we're assigning an HTML tag to a variable that is not a string but just HTML code.&lt;/p&gt;

&lt;p&gt;So to convert it to browser understandable JavaScript code, we use a tool like Babel which is a JavaScript compiler/transpiler.&lt;br&gt;
You can set up your own babel configuration using Webpack as I show in this article. Or you can use create-react-app which internally uses Babel for the JSX to JavaScript conversion.&lt;/p&gt;

&lt;p&gt;React jsx code like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;App /&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
  document.getElementById('root')
);
&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;export default function App( ) {
return &amp;lt;h1&amp;gt;hello world &amp;lt;/h1&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;# Component lifecycle :&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;In ReactJS, every component creation process involves various lifecycle methods. These lifecycle methods are termed components’ lifecycle. These lifecycle methods are not very complicated and are called at various points during a component's life. The lifecycle of the component is divided into four phases. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initial Phase&lt;/li&gt;
&lt;li&gt;Mounting Phase&lt;/li&gt;
&lt;li&gt;Updating Phase&lt;/li&gt;
&lt;li&gt;Unmounting Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each phase contains some lifecycle methods that are specific to the particular phase. Let us discuss each of these phases one by one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Initial Phase&lt;/strong&gt;&lt;br&gt;
It is the birth phase of the lifecycle of a ReactJS component. Here, the component starts its journey on a way to the DOM. In this phase, a component contains the default Props and initial State. These default properties are done in the constructor of a component. The initial phase only occurs once and consists of the following methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;getDefaultProps()&lt;/li&gt;
&lt;li&gt;getInitialState()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Mounting Phase&lt;/strong&gt;&lt;br&gt;
In this phase, the instance of a component is created and inserted into the DOM. It consists of the following methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillMount()&lt;/li&gt;
&lt;li&gt;componentDidMount()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Updating Phase&lt;/strong&gt;&lt;br&gt;
It is the next phase of the lifecycle of a react component. Here, we get new Props and change State. This phase also allows to handle user interaction and provide communication with the components hierarchy. The main aim of this phase is to ensure that the component is displaying the latest version of itself. Unlike the Birth or Death phase, this phase repeats again and again. This phase consists of the following methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentWillRecieveProps()&lt;/li&gt;
&lt;li&gt;shouldComponentUpdate()&lt;/li&gt;
&lt;li&gt;componentWillUpdate()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;componentDidUpdate()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Unmounting Phase&lt;/strong&gt;&lt;br&gt;
It is the final phase of the React component lifecycle. It is called when a component instance is destroyed and unmounted from the DOM. This phase contains only one method and is given below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;componentWillUnmount()&lt;/strong&gt;&lt;br&gt;
This method is invoked immediately before a component is destroyed and unmounted permanently. It performs any necessary cleanup-related task such as invalidating timers, event listener, canceling network requests, or cleaning up DOM elements. If a component instance is unmounted, you cannot mount it again.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;# Context Api : *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Context provides a way to pass data through the component tree without having to pass props down manually at every level.&lt;/p&gt;

&lt;p&gt;For a more in-depth definition, it provides a way for you to make particular data available to all components throughout the component tree no matter how deeply nested that component may be.&lt;/p&gt;

&lt;p&gt;example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  return(
    &amp;lt;ParentComponent theme = "light"/&amp;gt;
  );
}

const ParentComponent = (props) =&amp;gt; (
  &amp;lt;Child theme = {props.theme} /&amp;gt;
)

const Child = (props) =&amp;gt; (
  &amp;lt;Grandchild theme = {props.theme} /&amp;gt;
)

const Grandchild = (props) =&amp;gt; (
  &amp;lt;p&amp;gt;Theme: {props.theme}&amp;lt;/p&amp;gt;
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we specified the application theme using a props in the ParentComponent called theme. We had to pass that props to all components down the component tree to get it where it is needed which is the GrandChild component. The ChildComponent had nothing to do with the theme props but was just used as an intermediary.&lt;/p&gt;

&lt;p&gt;Now, imagine the GrandChild component was more deeply nested than it was in the top example. We would have to pass the theme props the same way we did here which would be cumbersome. This is the problem that Context solves. With Context, every component in the component tree has access to whatever data we decide to put in our context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Create with context api :&lt;/strong&gt;&lt;br&gt;
To create a context, we use React.createContext which creates a context object. You can pass in anything as an argument to React.createContext. In this case, we are going to pass in a string which is the current theme mode. So now our current theme mode is the “light” theme mode.&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";

const ThemeContext = React.createContext("light");
export default ThemeContext;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make this context available to all our React components, we have to use a Provider. What is a Provider? According to the React documentation, every context object comes with a Provider React component that allows consuming components to subscribe to context changes. It is the provider that allows the context to be consumed by other components. That said, let us create our provider.&lt;/p&gt;

&lt;p&gt;Go to your App.js file. In order to create our provider, we have to import our ThemeContext.&lt;/p&gt;

&lt;p&gt;Once the ThemeContext has been imported, we have to enclose the contents of our App component in ThemeContext.Provider tags and give the ThemeContext.Provider component props called values which will contain the data we want to make available to our component tree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const theme = "light";
  return (
    &amp;lt;ThemeContext.Provider value = {theme}&amp;gt;
      &amp;lt;div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/ThemeContext.Provider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
