Skip to main content

Kiểm tra giữa kỳ

Khởi tạo một project mới và thực hiện những yêu cầu sau:

Cấu hình (3 đ)

  1. Cài đặt thư viện & cấu hình nodemon:

    1. Môi trường dev (devDependencies): nodemon
    2. Môi trường production (dependencies): dotenv, express, jsonwebtoken
    3. Cấu hình nodemon trong scripts section của package.json (Kết quả mong muốn: gõ npm run dev để chạy nodemon)
  2. Tạo file .env với nội dung sau:

API_Token = "ORR7z3KMKceWMK5xIFUNLlrFceXN"
JWT_Secret = "toYfwSSniETCVTs6"
  1. Tạo router tương ứng với từng mục:
    • simple
    • posts
    • auth

Triển khai API (7 đ)

simple router

  1. /submit-formdata
    • Method: POST
    • Header:
      • Content-type: application/x-www-form-urlencoded
    • Body: name=John&age=18
    • Response:
      • 200:
        {
        "data": {
        "name": "John",
        "age": 18
        }
        }
      • 400: Nếu body không đủ thông tin nameage
  2. /submit-json
    • Method: POST
    • Header:
      • Content-type: application/json
    • Body: {"name": "Bob", "class": "NodeJS"}
    • Response:
      • 200:
        {
        "data": {
        "name": "Bob",
        "class": "NodeJS"
        }
        }
      • 400: Nếu body không đủ thông tin nameage
  3. /get-data
    • Method: GET
    • Header:
      • X-API-KEY: [API_Token]
    • Response:
      • 200:
        {
        "message": "Authorized"
        }
      • 401: Nếu request không có API_Token / API_Token không đúng

auth router

JWT Structure
  • Expire: 5mins
  • Payload:
{
"email": "<user-email>",
"role": "<user-role>"
}
  1. /register:

    • Method: POST
    • Header:
      • Content-type: application/json
    • Body: {"username": "<your-user-name>", "password": "<your-password>", "role": "<user-role>", "email": "<user-email>"}
    • Response:
      • 200:
        {
        "message": "Account created"
        }
      • 400: Nếu body không đủ thông tin
  2. /login:

    • Method: POST
    • Header:
      • Content-type: application/json
    • Body: {"username": "<exist-username>", "password": "<user-password>}
    • Response:
      • 200:
        {
        "token": "<JWT Token>"
        }
      • 400: Nếu body không đủ thông tin
JWT sign
const token = jwt.sign(payload, SECRET_KEY, { expiresIn: "1h" });

posts router

Post class
{
"id": int #increment,
"title": string,
"description": string,
"author_email": string #user-email
}
  1. /
    • Method: GET
    • Header:
      • Authorization: Bearer <jwt-token>
    • Response:
      • 200:
        		{
        "data": [list of post]
        }
      • 401: Nếu request không có token / token bị hết hạn
  2. /
    • Method: POST
    • Header:
      • Authorization: Bearer <jwt-token>
    • Body: {"title": "<title>", "description" : "<description>"}
    • Response:
      • 200:
        {
        "message": "created"
        }
      • 401: Nếu request không có token / token bị hết hạn
      • 400: Nếu body bị thiếu thông tin
JWT verify
jwt.verify(token, SECRET_KEY, (err, user) => {
/*callback*/
});

Nộp bài

  • Link nộp bài (yêu cầu đăng nhập tài khoản của trường)

  • Nén file và đặt tên theo cú pháp: MSSV_Ten

Chỉ nộp những file sau đây
  1. File chứa code (server.js, routers)
  2. package.json
  3. .env

Notes: Nộp sai file / định dạng sẽ bị hủy kết quả thi