Youcef Madadi
Web and game development teacher
Chapter 6 : REST TO 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.
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.
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.
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.
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 |
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")
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")
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
Using environment variables
if (!process.env.PORT) require("dotenv").config();
> npm i dotenv --save-dev
Install
Use
Install
Using authentication
By Youcef Madadi