Chúng ta có thể viết JavaScript trong nút JS không?

Nút. js tỏa sáng trong các ứng dụng web thời gian thực sử dụng công nghệ đẩy qua WebSocket. Các kết nối hai chiều, thời gian thực của nút---nơi mỗi máy khách và máy chủ có thể bắt đầu giao tiếp---cho phép trao đổi dữ liệu tự do hơn

Chia sẻ

Chia sẻ

Chúng ta có thể viết JavaScript trong nút JS không?

Nút. js tỏa sáng trong các ứng dụng web thời gian thực sử dụng công nghệ đẩy qua WebSocket. Các kết nối hai chiều, thời gian thực của nút---nơi mỗi máy khách và máy chủ có thể bắt đầu giao tiếp---cho phép trao đổi dữ liệu tự do hơn

Chúng ta có thể viết JavaScript trong nút JS không?

Bởi Tomislav Capan

Chuyên gia đã được xác minh  về Kỹ thuật

Tomislav là Kiến trúc sư giải pháp, nhà phát triển và tư vấn kỹ thuật được AWS chứng nhận với hơn 10 năm kinh nghiệm. Tomislav có bằng thạc sĩ về máy tính

Trước nút. js, JavaScript chỉ chạy trong trình duyệt web (chẳng hạn như Chrome, Firefox và MSIE) đi kèm với công cụ JavaScript tích hợp. Nút. js là JavaScript Runtime Engine cho phép chúng tôi chạy JavaScript theo cách độc lập mà không cần sử dụng trình duyệt

Nút. js thường được sử dụng để lập trình máy chủ web, tôi. e. , JavaScript phía máy chủ. Điều này phổ biến vì bạn có thể sử dụng cùng một ngôn ngữ JavaScript để lập trình máy khách và máy chủ

Trang mẹ của Node. js là https. //nodejs. tổ chức/

Các tính năng quan trọng của Nodes. js là

  1. Nút. js chạy công cụ JavaScript V8 hiệu suất cao, cốt lõi của Google Chrome, bên ngoài trình duyệt
  2. Nút. js sử dụng lập trình không đồng bộ. Nó cung cấp một tập hợp các chức năng I/O không chặn không đồng bộ (với các từ khóa mới
    // Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
    npm install -g less
          // -g (or --global) for all users
          // Installed under /usr/local/lib/node_modules/less
          // A symlink lessc created in /usr/local/bin
     
    // Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
    npm install -g gulp
          // Installed under /usr/local/lib/node_modules/gulp
          // A symlink gulp created in /usr/local/bin
    8 và
    // Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
    npm install -g less
          // -g (or --global) for all users
          // Installed under /usr/local/lib/node_modules/less
          // A symlink lessc created in /usr/local/bin
     
    // Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
    npm install -g gulp
          // Installed under /usr/local/lib/node_modules/gulp
          // A symlink gulp created in /usr/local/bin
    9). Thay vì chặn luồng và lãng phí chu kỳ CPU chờ đợi, Node. js sẽ tạm dừng yêu cầu đầu tiên, tiếp tục với yêu cầu tiếp theo và tiếp tục hoạt động khi phản hồi của yêu cầu đầu tiên quay lại. Điều này cho phép một nút. js để chạy trong một quy trình duy nhất mà không cần tạo chuỗi mới cho mọi yêu cầu. một nút. máy chủ js có thể xử lý hàng nghìn kết nối đồng thời mà không phải chịu gánh nặng quản lý đồng thời luồng
  3. Bạn có thể lập trình máy khách và máy chủ bằng cùng một ngôn ngữ JavaScript
  4. Có sẵn một số lượng lớn thư viện, được quản lý bởi npm (Trình quản lý gói nút)
  5. Nút. js miễn phí, mã nguồn mở và chạy trên nhiều nền tảng (Linux, Windows, macOS)

Cài đặt

Cài đặt nút. js

Trong Windows
  1. Goto Node. trang web tải xuống js @ https. //nodejs. org/en/download/ ⇒ Chọn "LTS" (Hỗ trợ dài hạn) ⇒ Tải xuống "Windows Binary (
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    0)" và "64-bit" (e. g. , "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    1" - 18MB) ⇒ Giải nén vào thư mục bạn chọn, ký hiệu là "Node. thư mục cài đặt js"
  2. Thêm "Nút. js" vào biến môi trường PATH của bạn. Thấy ""
trong macOS

[LÀM]

Trong Ubuntu
  1. Tải xuống nút. js từ https. //nodejs. tổ chức/. Chọn nền tảng Linux (e. g. ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    2)
  2. Giải nén vào một thư mục bạn chọn (e. g. ,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    3 hoặc
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    4)
  3. Nút. js có hai tệp thực thi trong thư mục con
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    5 của nó.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    6 (công cụ JavaScript độc lập) và
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    7 (Node. js Trình quản lý gói). Thay vì thêm thư mục
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    5 vào PATH, bạn có thể tạo liên kết tượng trưng ở một trong các thư mục PATH. Ví dụ: tôi tạo liên kết tượng trưng trong
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    9 (nằm trong PATH), như sau.
    $ cd /usr/local/bin
     
    // Create symlinks, may need to sudo
    $ ln -s /path-to-nodejs/bin/node
    $ ln -s /path-to-nodejs/bin/npm
     
    // Set to executable, may need to sudo
    $ chmod +x node
    $ chmod +x npm

Biến môi trường NODE_PATH

Trong hệ thống Windows của tôi, Nút. js không thể định vị một số gói đã cài đặt của tôi, ngay cả với tùy chọn

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
0. Tôi đã giải quyết vấn đề này bằng cách thêm thư mục con "_______13_______1" vào biến môi trường NODE_PATH

Để đặt thư mục "

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
1" thành NODE_PATH

Lưu ý rằng NODE_PATH có thể chiếm nhiều thư mục, được phân tách bằng dấu chấm phẩy (

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
3) trong Windows hoặc dấu hai chấm (
cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
4) trong macOS/Linux

Bắt đầu với nút. js

Chạy nút. js trong chế độ dòng lệnh REPL (Đọc-Đánh giá-In)

Bắt đầu một CMD/Terminal/Bash-Shell. Khởi chạy nút. js

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D

Nút. js chạy ở chế độ REPL (Đọc-Đánh giá-In) - một trình vỏ ngôn ngữ tương tác. Nó nhận một đầu vào của người dùng, đánh giá và trả về kết quả

Thực thi tệp nguồn JavaScript

Chuẩn bị một JavaScript tên là "

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
5" như sau

Bạn có thể chạy Tập lệnh trong Nút. js như sau

node hello.js
hello, world

Cài đặt các Gói JavaScript/Nút khác bằng cách sử dụng npm (Trình quản lý gói nút)

Nút. js đi kèm với một chương trình có tên là npm (Trình quản lý gói nút), được sử dụng để duy trì các gói JavaScript

Bạn có thể dễ dàng cài đặt các gói nút khác thông qua npm (Trình quản lý gói nút). Ví dụ,

// Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
npm install -g less
      // -g (or --global) for all users
      // Installed under /usr/local/lib/node_modules/less
      // A symlink lessc created in /usr/local/bin
 
// Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
npm install -g gulp
      // Installed under /usr/local/lib/node_modules/gulp
      // A symlink gulp created in /usr/local/bin

Thông tin thêm về npm trong phần sau

Nút. js Bắt đầu bằng các ví dụ

ví dụ 1. Nút. js với tư cách là Máy chủ web

Tạo tệp JavaScript sau và lưu dưới dạng "

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
6" trong thư mục bạn chọn

1
2
3
4
5
6
7
8
9
10
11
12
13
14

Chạy "_______13_______6" bằng Node (như một chương trình độc lập) như sau, thao tác này sẽ bắt đầu quy trình máy chủ HTTP

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/

Khởi động trình duyệt web (Chrome, Firefox). URL vấn đề

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
8. Bạn sẽ thấy một thông báo hello-world

Để dừng máy chủ, nhấn Ctrl-C (hoặc Ctrl-Shift-C)

Làm thế nào nó hoạt động?
  1. Đọc "Thông tin cơ bản về HTTP", nếu bạn chưa quen với giao thức HTTP
  2. Trong Dòng 5,
    cd /path/to/JavaScript-Directory
    node app.js
    HTTP Server listening at http://127.0.0.1:3000/
    9 tạo một máy chủ HTTP mới và trả về
  3. Trong Dòng 11, máy chủ được đặt để lắng nghe trên cổng và tên máy chủ được chỉ định, thông qua
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    0. Khi máy chủ đã sẵn sàng, chức năng gọi lại được gọi, chức năng này sẽ chạy
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    1 để thông báo rằng máy chủ của chúng tôi đã sẵn sàng
  4. Bất cứ khi nào nhận được yêu cầu mới, sự kiện
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    2 được gọi, cung cấp hai đối tượng. một
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    2 (một đối tượng
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    4) và một
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    5 (một đối tượng
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    6)
  5. Trong Dòng 6, chúng tôi đặt thuộc tính phản hồi
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    7 thành 200 để biểu thị phản hồi thành công (i. e. , "VÂNG"). Trong Dòng 7, chúng tôi đặt
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    8 của tiêu đề phản hồi thành MIME
    // Create a project directory
    mkdir myExpressApp
    cd myExpressApp
    
    // Initialize npm, which creates "package.json"
    npm init    // accept the defaults
    
    // Install "express" package, under "node_modules" sub-directory
    npm install express
    9. Trong Dòng 8, chúng tôi viết đầu ra và đóng phản hồi qua
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    0
  6. Bạn có thể kích hoạt Trình gỡ lỗi F12, để kiểm tra các thông báo yêu cầu và phản hồi trong "Mạng"

Nút. khung web js

Thẩm quyền giải quyết. Giới thiệu về nút. js@https. //nodejs. nhà phát triển/

Nút. js là một nền tảng cấp thấp. Trong những năm qua, nhiều khung công tác đầy đủ đã được phát triển trên Node. js, chẳng hạn như

  • AdonisJs. Khung công tác toàn ngăn xếp tập trung cao độ vào công thái học, tính ổn định và sự tự tin của nhà phát triển
  • Thể hiện. Nó cung cấp một trong những cách đơn giản nhưng mạnh mẽ nhất để tạo một máy chủ web
  • làm nhanh. Một khung web tập trung cao độ vào việc cung cấp trải nghiệm tốt nhất cho nhà phát triển với ít chi phí nhất và kiến ​​trúc plugin mạnh mẽ
  • hapi. Một khung phong phú để xây dựng các ứng dụng và dịch vụ cho phép các nhà phát triển tập trung vào việc viết logic ứng dụng có thể tái sử dụng thay vì dành thời gian xây dựng cơ sở hạ tầng
  • gấu trúc. Nó được xây dựng bởi cùng một nhóm đằng sau Express, nhằm mục đích trở nên đơn giản hơn và nhỏ hơn, dựa trên nhiều năm kiến ​​thức
  • lặp lại. io. Giúp dễ dàng xây dựng các ứng dụng hiện đại yêu cầu tích hợp phức tạp
  • sao băng. Một khung công tác toàn ngăn cực kỳ mạnh mẽ, cung cấp cho bạn cách tiếp cận đẳng cấu để xây dựng ứng dụng bằng JavaScript, chia sẻ mã trên máy khách và máy chủ. Nó tích hợp với lib giao diện người dùng React, Vue và Angular. Cũng có thể được sử dụng để tạo các ứng dụng dành cho thiết bị di động
  • siêu nhỏ. Nó cung cấp một máy chủ rất nhẹ để tạo các dịch vụ siêu nhỏ HTTP không đồng bộ
  • NestJS. Nút lũy tiến dựa trên TypeScript. js để xây dựng các ứng dụng phía máy chủ hiệu quả, đáng tin cậy và có thể mở rộng cấp doanh nghiệp
  • Kế tiếp. js. Một khung để kết xuất các ứng dụng React được kết xuất phía máy chủ
  • Nx. Nó cung cấp năng lượng cho Angular CLI, cho phép xây dựng các ứng dụng toàn ngăn xếp bằng NestJS, Express và Angular và dễ dàng chia sẻ mã giữa các phụ trợ và giao diện người dùng
  • Ổ cắm. io. Một công cụ giao tiếp thời gian thực để xây dựng các ứng dụng mạng

ví dụ 2. Thiết lập Máy chủ Web "Express"

Thẩm quyền giải quyết. "Thể hiện. Khung web tối giản, nhanh chóng, đơn giản dành cho Node. js" @ https. // expressjs. com/

Ví dụ này minh họa cách dễ dàng thiết lập máy chủ web Node chức năng bằng gói "Express"

Đầu tiên, tạo một thư mục dự án (giả sử "_______38_______1"), khởi tạo và cài đặt mô-đun "

1
2
3
4
5
6
7
8
9
10
11
12
2"

// Create a project directory
mkdir myExpressApp
cd myExpressApp

// Initialize npm, which creates "package.json"
npm init    // accept the defaults

// Install "express" package, under "node_modules" sub-directory
npm install express
ví dụ 1. Thiết lập Máy chủ HTTP cho Tài nguyên Tĩnh (HTML/CSS/JS/Hình ảnh/phông chữ)

Tạo tập lệnh sau trong thư mục dự án của bạn

1
2
3
4
5
6
7
8
9
10
11
12

Tạo một thư mục con tên là "______38_______3". Đặt một số tài nguyên tĩnh, chẳng hạn như "

1
2
3
4
5
6
7
8
9
10
11
12
4", "
1
2
3
4
5
6
7
8
9
10
11
12
5", "_______38_______6", trong thư mục
1
2
3
4
5
6
7
8
9
10
11
12
3

Chạy ứng dụng

node app.js
Server listening at port 3000...

Hãy thử các URL sau. Máy chủ HTTP "nhanh" này có thể phục vụ các tài nguyên tĩnh

http://localhost:3000/
http://localhost:3000/hello.html
http://localhost:3000/test.txt
http://localhost:3000/image.png
http://localhost:3000/something.html  // 404 Not Found
http://localhost:3000/static/hello.html
http://localhost:3000/static/test.txt
http://localhost:3000/static/image.png

Làm thế nào nó hoạt động?

  1. Ứng dụng này bắt đầu một máy chủ HTTP lắng nghe trên cổng 3000
  2. Trong Dòng 7, chúng tôi đã thiết lập một lộ trình cho yêu cầu của
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    8 tại
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    9 (thư mục cơ sở), để trả về thông điệp từ lời chào. Cú pháp là
  3. Trong Dòng 9, chúng tôi ánh xạ URL
    node app.js
    Server listening at port 3000...
    0 (root) tới thư mục của máy chủ vật lý "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    3". Bạn có thể sử dụng nhiều câu lệnh để bao gồm nhiều thư mục trong thư mục gốc, ví dụ:
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    0
  4. Trong Dòng 10, chúng tôi cũng ánh xạ URL
    node app.js
    Server listening at port 3000...
    2 tới thư mục của máy chủ vật lý "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    3" (chỉ để minh họa)
  5. Đối với bất kỳ URL nào khác, máy chủ sẽ trả về "Không tìm thấy 404"

[TODO] Trình tạo ứng dụng nhanh. Xem https. // expressjs. com/vi/khởi động/máy phát điện. html

mô-đun

Các mô-đun là Thư viện JavaScript, có sẵn cho Nút. js

Mô-đun tích hợp

Nút. js đi kèm với một tập hợp các mô-đun tích hợp. Để bao gồm một mô-đun tích hợp sẵn, hãy sử dụng hàm

node app.js
Server listening at port 3000...
4, e. g. ,

Viết mô-đun của riêng bạn

Để cung cấp các đối tượng của bạn trong JavaScript, hãy sử dụng

node app.js
Server listening at port 3000...
5. Ví dụ: tạo một JavaScript có tên "
node app.js
Server listening at port 3000...
6", như sau

Bạn có thể tham khảo các đối tượng đã xuất qua

node app.js
Server listening at port 3000...
7. Ví dụ: tạo một JavaScript có tên "
node app.js
Server listening at port 3000...
8", như sau

Sử dụng nút. js để chạy JavaScript

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
1

Mô-đun HTTP ('http')

Mô-đun HTTP tích hợp cho phép chúng tôi tạo một máy chủ web. Xem ví dụ trên

Bạn có thể tạo một máy chủ web trong một câu lệnh

Chạy

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
2

Khởi động trình duyệt web và đưa ra URL "

node app.js
Server listening at port 3000...
9"

Mô-đun hệ thống tệp ('fs')

Mô-đun Hệ thống tệp tích hợp cho phép chúng tôi thao tác với các tệp, chẳng hạn như

  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    0. mở tệp hoặc tạo tệp mới
  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    1
  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    2. ghi
    http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    3 vào
    http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    4, xóa nội dung hiện có
  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    5. nối thêm đã cho
    http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    3 vào
    http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    4
  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    8. xóa tài liệu
  • http://localhost:3000/
    http://localhost:3000/hello.html
    http://localhost:3000/test.txt
    http://localhost:3000/image.png
    http://localhost:3000/something.html  // 404 Not Found
    http://localhost:3000/static/hello.html
    http://localhost:3000/static/test.txt
    http://localhost:3000/static/image.png
    9
Thí dụ. Đọc tệp qua. Đọc tài liệu()

Ví dụ: chúng ta hãy tạo một máy chủ web đọc phản hồi từ một tệp

  1. Đầu tiên, tạo tệp HTML sau có tên "
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    4".
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    3
  2. Viết nút. ứng dụng web js
  3. Chạy ứng dụng, như trong ví dụ trên
I/O không đồng bộ

Hầu hết các hoạt động I/O trong Node. js không đồng bộ và không chặn, điều này tốt cho việc lập trình máy chủ web

Hãy thử JavaScript sau

Đầu ra là một "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
01" không mong muốn. Điều này là do
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
02 không đồng bộ. Cuộc gọi lại chỉ thực hiện sau khi tải tệp hoàn tất. Nút sẽ không đợi mà tiếp tục câu lệnh tiếp theo

Trong

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
03 không đồng bộ, bạn cần xử lý dữ liệu bên trong cuộc gọi lại

Bạn có thể (a) đặt mã xử lý của mình bên trong hàm gọi lại của

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
03 hoặc (b) xác định hàm và gọi hàm bên trong hàm gọi lại, ví dụ:

Phiên bản đồng bộ có tên là

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
05 có sẵn, ví dụ:

Thí dụ. . writeFile() và. appendFile()

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
06 xóa nội dung hiện có, trong khi
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
07 nối sau nội dung hiện có

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
4
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
5

Mô-đun URL ('url')

Mô-đun URL có thể được sử dụng để phân tích một URL. Ví dụ,

ví dụ 3. Máy chủ HTTP tĩnh đơn giản sử dụng 'http', 'url' và 'fs'

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
6
Làm thế nào nó hoạt động?
  1. Trong Dòng 7-8, chúng tôi sử dụng
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    08 để lấy tên tệp được yêu cầu
  2. Nếu không tìm thấy tệp, cuộc gọi lại sẽ trả về một
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    09, với mã trạng thái 404
  3. nếu tệp được tìm thấy, cuộc gọi lại sẽ ghi dữ liệu tệp, với mã trạng thái 200
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    10

Mô-đun Sự kiện ('sự kiện') để Quản lý Nút. sự kiện js

Các đối tượng trong nút. js có thể kích hoạt các sự kiện. Ví dụ: đối tượng

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
11 kích hoạt sự kiện khi mở và đóng tệp

Nút. js có một mô-đun tích hợp, được gọi là "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
12", nơi bạn có thể tạo, kích hoạt và đính kèm trình nghe vào sự kiện. Ngoài ra, tất cả các thuộc tính và phương thức sự kiện là thể hiện của đối tượng
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
13. Bạn cần tạo một thể hiện của đối tượng
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
13. Ví dụ,

Mô-đun NodeMailer ('nodemailer') để gửi email

Mô-đun "Nodemailer" đơn giản hóa việc gửi email từ Node. js

Để cài đặt "Nodemailer"

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
7

Mô-đun được cài đặt bên dưới "

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
1" của Nút. thư mục cài đặt js

Ghi chú lỗi. Nếu bạn gặp phải "lỗi. không tìm thấy mô-đun", bạn có thể thử thêm thư mục "

cd /path/to/JavaScript-Directory
node app.js
HTTP Server listening at http://127.0.0.1:3000/
1" vào biến môi trường NODE_PATH. Thấy ""

JavaScript sau đây có thể được sử dụng để gửi email

Bạn cần kiểm tra tên máy chủ/cổng của máy chủ SMTP và liệu SSL/TLS có được sử dụng hay không

Đối với "gmail", tên máy chủ/cổng của máy chủ SMTP được hiển thị. Bạn cũng cần phải "Đã bật ứng dụng kém an toàn hơn". Đi tới "Quản lý tài khoản google của bạn" ⇒ bảo mật ⇒ "Truy cập ứng dụng kém an toàn hơn" ⇒ "bật"

Nút. js Lập trình cơ sở dữ liệu

mysql

Thẩm quyền giải quyết. mô-đun mysql @ https. //www. npmjs. com/gói/mysql

Tôi cho rằng bạn đã quen thuộc với MySQL. Nếu không, hãy đọc "Cách cài đặt MySQL và bắt đầu"

Đầu tiên, tạo một thư mục dự án (giả sử "_______18_______17"), khởi tạo và cài đặt mô-đun trình điều khiển "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
18" của Node

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
8

(Đối với MySQL 8) MySQL 8 sử dụng phương thức xác thực

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
19 mới, không được hỗ trợ bởi mô-đun "mysql" của Node. Bạn cần hoàn nguyên về phương pháp
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
20 cũ, bằng cách đưa ra các lệnh này

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
9
ví dụ 1. Kết nối với máy chủ cơ sở dữ liệu MySQL
node hello.js
hello, world
0

Bắt đầu CMD/Terminal/Bash và khởi chạy máy chủ MySQL

node hello.js
hello, world
1

Bắt đầu CMD/Terminal/Bash "khác" để chạy JavaScript

node hello.js
hello, world
2

Làm thế nào nó hoạt động?

ví dụ 2. TẠO BẢNG, CHỌN, CHÈN, CẬP NHẬT và XÓA

Đầu tiên, sử dụng ứng dụng khách MySQL để tạo cơ sở dữ liệu có tên "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
21"

node hello.js
hello, world
3

Viết và chạy JavaScript

node hello.js
hello, world
4

Làm thế nào nó hoạt động?

ví dụ 3. Tuyên bố đã chuẩn bị (để ngăn chặn SQL injection)
node hello.js
hello, world
5

Làm thế nào nó hoạt động?

Ví dụ 4. Cam kết và khôi phục

[LÀM]

MongoDB

Tôi cho rằng bạn đã quen thuộc với MongoDB. Nếu không, hãy đọc "MongoDB Bắt đầu"

Đầu tiên, tạo một thư mục dự án (giả sử "_______18_______22"), khởi tạo và cài đặt mô-đun trình điều khiển MongoDB "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
23" của Node

node hello.js
hello, world
6
ví dụ 1. Kết nối với máy chủ cơ sở dữ liệu MongoDB

[LÀM]

NPM (Trình quản lý gói nút)

npm (Node Package Manager) là công cụ quan trọng nhất để làm việc với các ứng dụng Node. Nó quản lý các gói Node và JavaScript, đồng thời cũng có thể được sử dụng để chạy thử nghiệm và các công cụ được sử dụng trong quá trình phát triển. npm được cài đặt cùng với Node. js. Nó bao gồm một chương trình, còn được gọi là

1
2
3
4
5
6
7
8
9
10
11
12
13
14
7, và một sổ đăng ký để quản lý các gói

npm sử dụng tệp định nghĩa có tên "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
25" để quản lý gói phụ thuộc. Nó liệt kê tất cả các phụ thuộc của một gói, bao gồm tên, phiên bản, mô tả, tệp ban đầu để thực thi, phụ thuộc sản xuất, phụ thuộc phát triển, phiên bản Node có thể hoạt động, v.v. Nó chứa mọi thứ mà npm cần để tìm nạp và chạy ứng dụng của bạn

Thiết lập một dự án nút

  1. Tạo một thư mục dự án, chẳng hạn như
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    26.
    node hello.js
    hello, world
    7
  2. Sử dụng "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    27" để tạo tệp "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    25" cho ứng dụng này. Nó sẽ nhắc bạn về một số thứ. Chấp nhận mặc định trong thời gian này.
    node hello.js
    hello, world
    8Kiểm tra "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    25" được tạo trong thư mục dự án
    Ghi chú. Bạn cũng có thể khởi tạo Git thông qua "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    30"
  3. Cài đặt các gói mong muốn của bạn thông qua "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    31", ví dụ: "express".
    node hello.js
    hello, world
    9Kiểm tra "
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    25", đã thêm các thành phần phụ thuộc.
    // Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
    npm install -g less
          // -g (or --global) for all users
          // Installed under /usr/local/lib/node_modules/less
          // A symlink lessc created in /usr/local/bin
     
    // Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
    npm install -g gulp
          // Installed under /usr/local/lib/node_modules/gulp
          // A symlink gulp created in /usr/local/bin
    0Gói được cài đặt trong thư mục con "
    cd /path/to/JavaScript-Directory
    node app.js
    HTTP Server listening at http://127.0.0.1:3000/
    1" của thư mục dự án
  4. Để sử dụng mô-đun, hãy gọi hàm require() để đưa nó vào ứng dụng của bạn.
    // Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
    npm install -g less
          // -g (or --global) for all users
          // Installed under /usr/local/lib/node_modules/less
          // A symlink lessc created in /usr/local/bin
     
    // Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
    npm install -g gulp
          // Installed under /usr/local/lib/node_modules/gulp
          // A symlink gulp created in /usr/local/bin
    1

Thông tin thêm về "cài đặt npm"

Tùy chọn toàn cầu (--global hoặc -g)
// Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
npm install -g less
      // -g (or --global) for all users
      // Installed under /usr/local/lib/node_modules/less
      // A symlink lessc created in /usr/local/bin
 
// Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
npm install -g gulp
      // Installed under /usr/local/lib/node_modules/gulp
      // A symlink gulp created in /usr/local/bin
2
  • Nếu không có tùy chọn
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    34 (hoặc
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    35), gói sẽ được cài đặt trong dự án hiện tại trong thư mục con "
    cd /path/to/JavaScript-Directory
    node app.js
    HTTP Server listening at http://127.0.0.1:3000/
    1". Do đó, nó chỉ khả dụng cho dự án hiện tại
  • Với tùy chọn
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    34 (hoặc
    node
    Welcome to Node.js v12.16.1.
    Type ".help" for more information.
    > .help
    .break    Sometimes you get stuck, this gets you out
    .clear    Alias for .break
    .editor   Enter editor mode
    .exit     Exit the repl
    .help     Print this help message
    .load     Load JS from a file into the REPL session
    .save     Save all evaluated commands in this REPL session to a file
    Press ^C to abort current expression, ^D to exit the repl
    
    > console.log("hello, world")
    hello, world
    undefined   // return value of console.log() function
    > .exit     // or Ctrl-C twice, or Ctrl-D
    
    35), gói sẽ được cài đặt trong thư mục con "
    cd /path/to/JavaScript-Directory
    node app.js
    HTTP Server listening at http://127.0.0.1:3000/
    1" của Node. Nó có sẵn cho tất cả các dự án. Hãy lưu ý rằng bạn có thể cần đặt NODE_PATH thành thư mục "
    cd /path/to/JavaScript-Directory
    node app.js
    HTTP Server listening at http://127.0.0.1:3000/
    1" chung
Sử dụng các gói cài đặt cục bộ

Từ npm@5. 2. 0, npm đi kèm với gói npx cho phép bạn chạy các lệnh từ mô-đun nút cục bộ hoặc từ mô-đun toàn cầu

// Install Less, a CSS preprocessor. May need to use "sudo" to write to the global directory
npm install -g less
      // -g (or --global) for all users
      // Installed under /usr/local/lib/node_modules/less
      // A symlink lessc created in /usr/local/bin
 
// Install Gulp, an automation tool. May need to use "sudo" to write to the global directory
npm install -g gulp
      // Installed under /usr/local/lib/node_modules/gulp
      // A symlink gulp created in /usr/local/bin
3

Theo mặc định, npx sẽ kiểm tra xem tồn tại trong PATH hay trong các tệp nhị phân của dự án cục bộ và thực thi điều đó

Gọi

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
41 khi
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
42 chưa có trong PATH của bạn sẽ tự động cài đặt một gói có tên đó từ sổ đăng ký NPM cho bạn và gọi nó. Bạn có thể ngăn chặn hành vi này bằng cách cung cấp tùy chọn
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
43

[TODO] thêm về npx

Tùy chọn phát triển (--save-dev)

Một số gói, chẳng hạn như những gói dành cho thử nghiệm, không cần thiết để triển khai sản xuất. Bạn có thể sử dụng tùy chọn

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
44 để chỉ đánh dấu nó để phát triển. Trong "
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
25", nó sẽ được liệt kê dưới "
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
46" thay vì "_______18_______47", e. g. ,

tập lệnh chạy npm

Bạn cũng có thể định cấu hình npm dưới dạng tốc ký để chạy tập lệnh hoặc lệnh, thông qua "

node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
48", trong phần "
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
49" của "
node
Welcome to Node.js v12.16.1.
Type ".help" for more information.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

> console.log("hello, world")
hello, world
undefined   // return value of console.log() function
> .exit     // or Ctrl-C twice, or Ctrl-D
25"

Tôi có thể sử dụng JavaScript trong nút JS không?

Nút. js cho phép bạn chạy JavaScript trên máy chủ .

Làm cách nào để chạy tệp JavaScript trong nút js?

Cách thông thường để chạy một Nút. js là chạy lệnh node có sẵn trên toàn cầu (sau khi bạn cài đặt Node. js) và chuyển tên tệp bạn muốn thực thi . Trong khi chạy lệnh, đảm bảo rằng bạn đang ở trong cùng thư mục chứa ứng dụng.