Skip to content
youhoc
  • Pages
    • Home
    • Modern App Guidelines
    • Linux
      • Day 1: Linux Distributions & Navigation
      • Day 2: User Management
      • Day 3: File Permission & Ownership
      • Day 4: Package Management
      • Day 5: Services Management
    • Javascript
      • JS The Weird Part
        • Execution Context
        • Types & Operators
        • Objects & Functions
        • Error Handling & Strict Mode
        • Typescript, ES6, Tra
      • Modern JS
        • JS in the Browser
        • Data Storage JSON
        • Modern JS
        • Advanced Objects & Methods
        • Webpack & Babel
        • Async
      • jQuery
        • In-depth Analysis of jQuery
      • React-ready JS
        • Arrow Function
        • Template Literals
        • Logical AND, OR, Ternary, Nullish Operators
        • Destructuring & Rest Operator
        • Array Method
        • Immutability and Spread Operator
        • Promises, Async/Await, Callback
    • PHP
      • gruntJS
      • composer
      • MySQL
    • Docker
      • Container Basics
      • Container Networking
      • Container Image
      • Container Volume & Persistent Data
      • Dockerfile
      • Docker Compose
      • Docker Registry
    • Node.js
      • 1. Installing & Exploring
      • 2. Core Modules
      • 3. Get User Input
      • File System & Input Arguments
      • 5. Express Web Server
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • icon picker
        8. Rest API
    • ReactJS
      • React from Andrew
        • Summary from Next
        • 1. Basics
        • 2. React Components
        • 3. Webpack
        • 4. Styling with SCSS
        • 5. React Router
        • 6. React Hook
      • Modern React From The Beginning
        • Intro to JSX
        • Vite Build Tools
        • Basic Component Creation
        • Component State
        • Props & Component Composition
        • useState with Inputs & Form Submission
        • useEffect, useRef & Local Storage
        • Async / Await and Http Request in React
        • React Router: Declarative Mode
        • ContextAPI
        • React Router: Framework Mode
          • File-routing & HTML Layouts
          • Server-side Data Query
          • Links & Navigation
          • Loaders
    • Typescript
      • Type User vs UserProp
    • Payload CMS

8. Rest API

Basic

Representational State Transfer - Application Programming Interface
pre-defined operation
GET - Read
POST - Create
to resources

HTTP Request


HTTP Request is made-of:
request: method + endpoint + http
header: accept, authorization, connection
body: JSON data
Response Data is made-of:
method and status code
header
body

Download and Install Postman

Endpoint Setup Route

Use:
express
routing
Return Status COde
Mongoose Queries

GET
POST
PATCH
when update a document, strange fields (which are not in model) will be ignored.
how to catch errors when fields do not exist, fields are locked (like _id)

Multiple Express Router

Separate Router File for seperate resources

Middleware

Sections:

Express Middleware

new request — express router handler
new request — middleware do something — express router handler
New Request:
Header:
Middleware function
create a folder named ‘middleware’
put a function in it (req,res,next)
require that function in according router (routers/user.js)

Authentication Middleware:

Mongoose Middleware

pre save() action
can’t use findByIdAndUpdate, must convert Object to Associative Array

Properties of JavaScript objects can also be accessed or set using a bracket notation (for more details see ). Objects are sometimes called associative arrays, since each property is associated with a string value that can be used to access it. So, for example, you could access the properties of the myCar object as follows:
Assign a static function to Schema

JWT Web Token

public accessible by anyone
private, only for logged in users

JWT includes 3 part:
header part
user data part
this part can be decoded using base64 decode
random string
DONT LOSE THE WHOLE JWT TOKEN
JWT should:
expire: use
not expire
both expiration date save in JWT token

Postman Advanced

Create enviroments
Task Manager (dev)
Task Manager (production)
Create env var
Use env var as {{env_var}}
Use or Not Use Authorization
setup Authorization at parent (Category Level)
choose “Inherit auth from parent” from Authorization
no longer need authorization header
if not use, choose, “no authorization”

Use PM JS to generate login user token and pass it onto env var
to simulate real life usage

Hiding Private Data

Never send back private data in JSON
password
tokens array
res.send() will JSON.stringlify any object into JSON
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.