DEV Community

Cover image for CURD operations, NodeJs, JWT
Md Shah Jalal
Md Shah Jalal

Posted on

CURD operations, NodeJs, JWT

What is the CURD operations?

When we create a project with React as a client site and with NodeJs as a server site, we have to process some operations on the server site with NodeJs. CURD is an acronym that stands for Create, Update, Read, and Delete. According to our needs, We use to get, post, put, delete methods.

What is NodeJs?

Node.js is an open-source, cross-platform, back-end, JavaScript runtime build on Chrome V8 engine that can execute JavaScript code outside of web browser.

Node.js runs single-threaded, non-blocking, asynchronous programming, which is very memory efficient.

Important Note:

  • NodeJs is a separate or another runtime of JavaScript.
  • NASA, Uber, IBM, Paypal use NodeJs.
  • The V8 engine makes JavaScript fast Just in time (JIT) compile.
  • npm stands for Node Package Manager.
  • MongoDB keeps the data in JSON structure.

How does NodeJs work?

NodeJs is a JavaScript runtime built on Chrome V8 engine that can execute JavaScript code outside of the browser. Language and syntax remain in the something.js file as content. In the browser, this content is called DOM. It stands for document object model. DOM is a huge JavaScript object.

When a client site request data to a server-side, If the server-side sends a response on its own, it is called CPU Intensive Task.
And if the server-side sends a response by collecting data from others, it is called I/O Intensive Task.

NodeJs runs as single-threaded that is just a process, asynchronously and non-blocking I/O.

  • Non-blocking: Single-threaded accepts the request and then sends the request to others. The current thread won’t remain blocked with the request.
  • Asynchronous/ Call back: NodeJs does not work synchronously or one by one.

Important Note:

  • Node.js can generate the dynamic page content
  • NodeJs is not suitable for CPU-intensive tasks.
  • We use NodeJs only for I/O Intensive tasks.
  • Node.js can create, add, open, read, write, delete, update data, and close files on the server.

What is JWT?

Most web apps use security measures to make sure user data stays private. Authentication is a key part of security and JSON Web Tokens (JWT) are a great way to implement authentication.

So what are JSON Web Tokens?

JWT is a standard that defines a compact and self-contained way to securely transmit information between a client and a server as a JSON object. The compact size makes the tokens easy to transfer through an URL, POST parameter, or inside an HTTP header. Also, since they are self-contained they include all the necessary information about a user so the database does not need to be queried more than once.
The information in a JWT can be trusted because it is digitally signed using a secret or public/private key pair.

Authentication

JWT is mainly used for authentication. After a user logs in to an application, the application will create a JWT and send it back to the user. Subsequent requests by the user will include the JWT. The token tells the server what routes, services, and resources the user is allowed to access. JWT can be easily used across multiple domains so they are often used for Single Sign-On.

Install

npm install react-jwt
or
yarn add react-jwt

Usage

import React from "react";
import { useJwt } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const { decodedToken, isExpired } = useJwt(token);
  /*
    If is a valid jwt, 'decodedToken' will be a object
    it could look like:
    {
      "name": "Gustavo",
      "iat": 1596408259,
      "exp": 4752168259
    }

    'isExpired' will return a boolean
    true => your token is expired
    false => your token is not expired
  */

  return (
    <div>
      ...
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

You can also use the methods isExpired(token) and decodeToken(token)

import React from "react";
import { isExpired, decodeToken } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const myDecodedToken = decodeToken(token);
  const isMyTokenExpired = isExpired(token);

  return (
    <div>
      ...
    </div>
  );
};
Enter fullscreen mode Exit fullscreen mode

Refresh state

If you use the refreshToken(newToken) method, useJwt's state will be updated

import React from "react";
import { useJwt } from "react-jwt";
const token = "Your JWT";

const Example = () => {
  const { decodedToken, isExpired, reEvaluateToken } = useJwt(token);

  const updateToken = () => {
    const newToken = "A new JWT";
    reEvaluateToken(newToken); // decodedToken and isExpired will be updated
  }

  return (
    <div>
      ...
    </div>
  );
};

Enter fullscreen mode Exit fullscreen mode

Oldest comments (0)