DEV Community

Sawda Hoque
Sawda Hoque

Posted on

Basic on JWT,MONGOOSE,NODE,EXPRESS

JWT

JSON Web Token is a proposed Internet standard for creating data with optional signature and/or optional encryption whose payload holds JSON that asserts some number of claims. The tokens are signed either using a private secret or a public/private key.

When should you use JSON Web Tokens?

Authorization: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.
Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

JSON Web Token structure:
Header:The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA.

Payload:The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.

Signature :To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

Mongoose

Mongoose is a JavaScript object-oriented programming library that creates a connection between MongoDB and the Express web application framework. Mongoose is a Node. js-based Object Data Modeling (ODM) library for MongoDB. It is akin to an Object Relational Mapper (ORM) such as SQLAlchemy for traditional SQL databases. The problem that Mongoose aims to solve is allowing developers to enforce a specific schema at the application layer.

Mongoose module

Mongoose.module is one of the most powerful external module of the node.js. Mongoose is a MongoDB ODM (Object database Modeling) that is used to translate the code and its representation from MongoDB to the Node.js server.

Advantages of Mongoose module:

  • Collection validation of the MongoDB database can be done easily.
  • Predefined Structure can be implemented on the collection.
  • Constraints can be applied to documents of collections using Mongoose.

Mongoose find() function

The find() function is used to find particular data from the MongoDB database. It takes 3 arguments and they are query (also known as a condition), query projection (used for mentioning which fields to include or exclude from the query), and the last argument is the general query options (like limit, skip, etc).

Mongoose findOne() function

The findOne() function is used to find one document according to the condition. If multiple documents match the condition, then it returns the first document satisfying the condition.

Mongoose update() function

The update() function is used to update one document in the database without returning it.

Mongoose updateOne() function

The updateOne() function is used to update the first document that matches the condition. This function is the same as update(), except it does not support the multi or overwrite options.

Mongoose updateMany() function

The updateMany() function is same as update(), except MongoDB will update all documents that match the filter. It is used when the user wants to update all documents according to the condition.

Mongoose remove() function

The remove() function is used to remove the documents from the database according to the condition.

Mongoose deleteOne() function

The deleteOne() function is used to delete the first document that matches the conditions from the collection. It behaves like the remove() function but deletes at most one document regardless of the single option.

Mongoose deleteMany() function

The deleteMany() function is used to delete all of the documents that match conditions from the collection. This function behaves like the remove() function but it deletes all documents that match conditions regardless of the single option,

Node.js

Nodejs is a server-side JavaScript run-time environment. It's open-source.

Difference between Nodejs and javascript

  • i.NodeJS is a Javascript runtime environment. But javascript is a programming language that is used for writing scripts on the website.
  • ii.We can run Javascript outside the browser with the help of NodeJS.But Javascript can only be run in the browsers.
  • iii.Nodejs does not have capability to add HTML tags,it is mostly used in sever side.
  • But Javascript is capable to add HTML and play with the DOM,it is basically used in clients side.

Differences between sql and nosql in database

  • i.SQL databases are relational that means relational database management system (RDBMS), NoSQL database is non-relational or distributed. SQL databases are table-based whereas NoSQL databases are document-based.
  • ii.SQL database the data is in the form of tables consisting of a number of rows, whereas data in NoSQL has no standard schema. NoSQL database have dynamic schema while SQL databases consist of predefined schema.
  • iii.Nosql are horizontally scalable while sql database are vertically scalable.

Express.js

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node based Web applications. Following are some of the core features of Express framework −

  • Allows to set up middlewares to respond to HTTP Requests.
  • Defines a routing table which is used to perform different actions based on HTTP Method and URL.
  • Allows to dynamically render HTML Pages based on passing arguments to templates.

Top comments (0)