Node JS

Chapter 6 : REST TO API

REST TO API

  1. What is a Restful API
  2. Requests Types
  3. Structuring our DB
  4. Routing our API
  5. Securing our API

1. What is a Restful API ?

What is an API ?

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other.

 

Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API.

1. What is a Restful API ?

What is REST?

REST, or Representational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other.

 

REST-compliant systems, often called RESTful systems, are characterized by how they are stateless and separate the concerns of client and server.

1. What is a Restful API ?

What stateless means?

Systems that follow the REST paradigm are stateless, meaning that the server does not need to know anything about what state the client is in and vice versa. In this way, both the server and the client can understand any message received, even without seeing previous messages.

1. What is a Restful API ?

What is a REST API?

A REST API (also known as RESTful API) is an application programming interface (API or web API) that conforms to the constraints of REST architectural style and allows for interaction with RESTful web services.

2. Requests Types

What are the restful routes ?

Name Path HTTP Verb Purpose
Index /users GET List all users
New /users/new GET Show the newest user
Create /users POST Create a new user
Show /users/:Id GET Show specific user
Edit /users/:Id/edit GET Show edit form for one user
Update /users/:Id PUT update a particular user. then redirect somewhere
Destroy /users/:Id DELETE delete a particular user. then redirect somewhere

2. Requests Types

HTTP Verbs in Express ?

usersRoute.get(function(req,res){
	// get users list
	res.send(users)
})

GET

usersRoute.post(function(req,res){
	// save user
	res.redirect("/users")  // or send a status msg
})

POST

const usersRoute = Router.route("/users")
	

2. Requests Types

HTTP Verbs in Express ?

usersRoute.put(function(req,res){
	// Update user
	res.redirect("/users")// or send a status msg
})

PUT

usersRoute.delete(function(req,res){
	// delete user
	res.redirect("/users") // or send a status msg
})

DELETE

const usersRoute = Router.route("/users/:Id")
	

2. Requests Types

Retrieving Data ?

app.use(express.text());

Text

app.use(express.urlencoded());

HTML Form

const express = require("express"),
    app = express();
//parsers
app.post("*", (req, res) => {
    console.log(req.body);
    res.send("done");
});
app.use(express.json());

JSON

3. Structuring our DB

  1. Choosing the collection we need for the project.
  2. Creating their schema
  3. Adding restrictions.
  4. Adding relations.
  5. Adding Methods and middleware.

4. Routing our API

  1. Choosing how to communicate and what the response should be.
  2. Implement routes
  3. Implement DB methods needed
  4. Adding authentication

5. Securing our API

  1. Using environment variables
  2. Using authentication
  3. Handling Errors  
  4. Responses status

5. Securing our API

Using environment variables

if (!process.env.PORT) require("dotenv").config();
> npm i dotenv --save-dev

Install

Use 

Install

5. Securing our API

Using authentication

REST TO API Ends

Node js Chapter 6

By Youcef Madadi

Node js Chapter 6

  • 319