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
      • Installing & Exploring
      • icon picker
        Loading Modules
      • npm - Get Command Input
      • Web Server
        • Express Web Server
      • Template Engine & MVC
      • File System & Input Arguments
      • 6. Deploy to Heroku & Github
      • Authentication
      • 7. Databases
      • 8. Rest API
      • Errors
    • 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
Share
Explore

Loading Modules

Core Modules

Nếu load những core modules (được list trên Documentation) thì chỉ cần gọi đúng tên khi load.
KHUYẾN KHÍCH đặt tên hằng (const) giống tên module (class).
Một số Core Modules phổ biến
http: create server, request & response
https: create SSL server, request & response
fs: file system
path: quản lý đường dẫn
os: hệ điều hành
Official List of Core Modules
Create a server with HTTP Core Module
Node.js Program Lifecycle:
continue running if these is an event listener (http.createServer listen for requests)
that event loop runs in a thread (leverage RAM), can handle many requests
use process.exit to stop that loop

Custom Modules

Nếu muốn load module ngoài (ở đây tạm hiểu là function) thì cần dùng hàm require.
Các biến trong file được require là local scope, không sử dụng được ở file hiện tại.
Cung cấp đường dẫn tương đối đến file được require
Trong app.js:
Trong function.js

(2.3) Nếu muốn load module từ thư viện npm

Đầu tiên phải initialize npm
chạy ở thư mục chứa app / dự án
câu lệnh tạo ra 1 file JSON lưu ở thư mục gốc của dự án, giống như file cấu hình
Tìm đến thư viện cần cài trên npmjs.com
chạy câu lệnh cài thử validator.js https://www.npmjs.com/package/validator

Sau khi chạy câu lệnh này thì Terminal sẽ làm 3 thứ
Tạo thư mục node_modules chứa tất cả script của thư viện
Tạo file package-lock.json chứa cấu hình của thư viện
Cập nhật package.json thêm giá trị dependencies, tên libs và version

KHÔNG NÊN ĐỤNG ĐẾN CÁC FILES NÀY
Khi xuất code cho đồng nghiệp, gỡ thư mục node_modules, đồng nghiệp có thể tự cài lại các package trong package.json và package-lock.json bằng cách chạy npm install

Để bắt đầu sử dụng lib này thì truyền giá trị vào hằng (thường bây giờ là class)
Có thể sử dụng cho dự án PHP, sử dụng JS ở client-side
Tổng hợp bài hôm nay trong 1 file

(2.4) Cài đặt global module trên máy chủ, không có dependencies, chạy trực tiếp từ hệ thống

Ví dụ, cài nodemon để không cần phải chạy lại app.js mỗi lần sửa code. Nodemon sẽ tự restart script khi tháy có thay đổi code.
Lưu ý:
Khi cài global module, thêm hậu tố -g
Do liên quan đến hệ thống (cài trên máy chủ), nếu báo lỗi thì thêm sudo (quyền admin)

Chạy quài 1 file app.js cho đến khi exit
Cách tạo function

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.