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 Show Chia sẻ Chia sẻ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 Bởi Tomislav CapanChuyê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à
Cài đặtCài đặt nút. jsTrong Windows
trong macOS[LÀM] Trong Ubuntu
Biến môi trường NODE_PATHTrong 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. jsChạ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 JavaScriptChuẩ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ủ webTạ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?
Nút. khung web jsThẩ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ư
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 122" // 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 124", " 1 2 3 4 5 6 7 8 9 10 11 125", "_______38_______6", trong thư mục 1 2 3 4 5 6 7 8 9 10 11 123 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?
[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ô-đunCác mô-đun là Thư viện JavaScript, có sẵn cho Nút. js Mô-đun tích hợpNú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-D1 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-D2 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ư
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
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-D01" 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-D02 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-D03 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-D03 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-D05 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-D06 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-D07 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-D4 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-D5 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-D6 Làm thế nào nó hoạt động?
Mô-đun Sự kiện ('sự kiện') để Quản lý Nút. sự kiện jsCá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-D11 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-D12", 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-D13. 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-D13. Ví dụ, Mô-đun NodeMailer ('nodemailer') để gửi emailMô-đ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-D7 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ệumysqlThẩ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-D18" 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-D8 (Đố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-D19 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-D20 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-D9 ví dụ 1. Kết nối với máy chủ cơ sở dữ liệu MySQLnode hello.js hello, world0 Bắt đầu CMD/Terminal/Bash và khởi chạy máy chủ MySQL node hello.js hello, world1 Bắt đầu CMD/Terminal/Bash "khác" để chạy JavaScript node hello.js hello, world2 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-D21" node hello.js hello, world3 Viết và chạy JavaScript node hello.js hello, world4 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, world5 Làm thế nào nó hoạt động? Ví dụ 4. Cam kết và khôi phục[LÀM] MongoDBTô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-D23" của Node node hello.js hello, world6 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 147, 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-D25" để 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
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/bin2
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/bin3 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-D41 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-D42 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-D43 [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-D44 để 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-D25", 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-D46" thay vì "_______18_______47", e. g. , tập lệnh chạy npmBạ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-D48", 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-D49" 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-D25" 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. |