Basic
Representational State Transfer - Application Programming Interface
HTTP Request
HTTP Request is made-of:
request: method + endpoint + http header: accept, authorization, connection Response Data is made-of:
Download and Install Postman
Endpoint Setup Route
Use:
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:
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
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:
this part can be decoded using base64 decode DONT LOSE THE WHOLE JWT TOKEN JWT should:
both expiration date save in JWT token
Postman Advanced
Create enviroments
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
res.send() will JSON.stringlify any object into JSON