Skip to content
This repository was archived by the owner on Jan 28, 2024. It is now read-only.

REST API with ExpressJS having SQL integration and JWT Authentication.

amritpandey23/REST-API-Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API

Code and notes in this repo follows this tutorial on udemy. However, major modification are made by me. The notes in this README file is also composed by me.

Stack

  • Node.js
  • Express.js
  • MySQL
  • MongoDB
  • GraphQL

📖 REST

  • REST stands for REpresentational State Transfer.
  • Representation is of a resource(data).
  • Transfer takes place from server to an application state.
  • REST is stateless i.e. state is not managed by server.

📖 Resources

  • Resources are key business decision. eg. payments, orders, products etc.
  • Resource entity is always declared in plural.
  • An entity is not a single object.
    http://domain.com/api/product/1     // wrong
    http://domain.com/api/products/1    // right
  • Entity can be composed of multiple data source.

📖 RESTful Application

  • Application that uses REST standards.
  • Utilise HTTP or similar protocol.
  • Uses HATEOAS.

📖 HTTP

  • HTTP stands for Hypertext Transfer Protocol.
  • It is used to send and retrive data through references in request and response objects.

👉 HTTP Responses

  • 10X : Information messages
  • 20X : Success messages
  • 30X : Redirection messages
  • 40X : Client error messages
  • 50X : Server error messages

👉 HTTP Methods

  • GET : requesting data
  • POST : creating data
  • PATCH : updating data
  • PUT : partially creating data
  • DELETE : deleting data
  • HTTP.GET and HTTP.DELETE are idempotent i.e. always does and returns same thing.
  • HTTP.POST, HTTP.PUT and HTTP.PATCH are non-idempotent.

📖 GraphQL

  • Query language for REST API.
  • Complements REST but not replace it.
  • Acts as a middle man between REST API response and Client.

📖 Authentication and Authorisation

  • Authentication is to verify a user
  • Authorisation is to verify access to a user.
  • Authentication is done via API keys, OAuth tokens or JWT.
  • OAuth stands for Open authentication.
  • JWT stands for JSON web tokens.
  • JWT have base 64 encoded data.
  • JWT = header.payload.signature.

📖 Resource Identifier

  • URI stands for Uniform Resource Identifier. eg.
http://domain.com/api/departments/1

📖 Express.js

  • Express is a Node.js web server.
  • It can handle routes.
  • Have HTTP utilities.
  • Have middlewares.

📖 Query Strings and Parameters

  • Following URI have a query paramter id.
http://domain.com/api/products/:id

Following URI have a query string argument orderBy.

http://domain/api/products?orderBy=price:ASV

📖 Relational Databases

  • RDMS was invented by Edgar F. Codd of IBM.

  • RDMS have following features:

    1. Tables
    2. Rows
    3. Columns
    4. Relationships
    5. Datatypes
    6. Keys: Primary, Foreign.
  • Relationships can be of following types:

    1. One-to-One
    2. One-to-Many
    3. Many-to-Many

📖 SQL

  • SQL stands for Structured Query Language.

  • SQL is used to manage RDMS databases like MySQL, MariaDB, PostgreSQL etc.

  • It has following features:

    1. Database manager.
    2. Table manager.
    3. Index manager.
    4. System manager.
    5. User manager.

💻 Mini SQL Tutorial

MySQL server is by default hosted on port 3306. Login to mysql shell with

$ mysql -u root -p

👉 Creating database

CREATE DATABASE my_db;

👉 Creating table

USE my_db;

CREATE TABLE my_table (
    id INT NOT NULL AUTO_INCREMENT,
    column1 VARCHAR(50),
    column2 DATETIME,
    PRIMARY KEY(id)
);

👉 Inserting data in table

INSERT INTO my_table
(column1, column2) VALUES
("something", "1980-09-22");

👉 Updating data in table

UPDATE my_table
SET column1 = "nothing"
WHERE id = 1;

👉 Deleting data from table

DELETE FROM my_table
WHERE id = 1;

👉 Query data from table

SELECT column1, column2 FROM my_table;

👉 Join two tables

SELECT a.column1, b.column1
FROM my_table AS a
INNER JOIN my_table2 AS b
ON a.id = b.id;

🚫 Key challenges with RDMS

  • Strict scheme.
  • Ever changing data requirements.
  • Data marts and lakes -- how to update them?

About

REST API with ExpressJS having SQL integration and JWT Authentication.

Topics

Resources

Stars

Watchers

Forks