DEV Community

meotism
meotism

Posted on • Updated on

Web's working principles

*Web's working principles theo 4 bước:

  • Client sử dụng giao thức TCP/IP để connect server.
  • Client gửi package HTTP request đến server.
  • Server trả về package HTTP response đến client. Nếu trong package request có chứa dynamic scripts, server sẽ chạy scripts trước.
  • Client nhận package HTTP response và mất kết nối từ server, bắt đầu rendering HTML. URLs: truy cập web pages. DNS: domain name server sẽ lấy địa chỉ IP của host. Các thông báo đến client:
  1. 1xx Informational
  2. 2xx Success
  3. 3xx Redirection
  4. 4xx Client Error
  5. 5xx Server Error

REST là các phương thức của truyền tải http:
GET - display <=> Read
POST - create <=> Create
PUT - update <=> Update
DELETE - delete <=> Delete


*Policy CORS
Cors là cơ chế hỗ trợ bảo mật cho các đường truyền request-origin và chuyển dữ liệu giữa client và servers. Nó cho phép truy cập across domain khác.

  • Lỗi CORS policy: Khi bạn call API tới server mà không có header Access-Control-Allow-Origin hoặc giá trị không hợp lệ thì phát sinh lỗi không lấy dữ liệu từ API.
  • Cách khắc phục: Enable CORS tại server Tại nginx: add_header Access-Control-Allow-Origin *;

Tại web application(ví dụ go):
func enableCors(w http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "
")
}
và thêm enableCors(&w) tại các hàm handler


Top comments (0)