Đầu vào NodeJS từ bảng điều khiển

Để yêu cầu người dùng nhập dữ liệu từ trình duyệt, bạn cần sử dụng phương thức

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
1 do trình duyệt cung cấp

Phương thức

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
1 cho phép bạn chấp nhận đầu vào của người dùng dưới dạng chuỗi và lưu trữ nó trên một biến như sau

const input = prompt();

Phương pháp này cũng chấp nhận một

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
3 làm thông tin bổ sung để cho người dùng biết loại đầu vào mà ứng dụng của bạn đang mong đợi

Ví dụ: viết đoạn mã sau để hỏi tên người dùng

const input = prompt("What's your name?");
alert(`Your name is ${input}`);

Hoặc viết gợi ý sau khi cần biết tuổi của người dùng

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
1 sẽ được trình duyệt hiển thị như sau

Browser prompt exampleVí dụ nhắc trình duyệt

Khi đó phương thức

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
5 sẽ hiển thị kết quả như sau

Alert showing the resultThông báo hiển thị kết quả

Bạn có thể tạo kiểu văn bản

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
3 dưới dạng câu hỏi hoặc gợi ý tùy theo yêu cầu của bạn

Nhận đầu vào của người dùng từ bảng điều khiển NodeJS

Để chấp nhận đầu vào của người dùng từ bảng điều khiển NodeJS, bạn cần sử dụng mô-đun

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
7 được cung cấp

Bạn có thể

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
8 mô-đun như sau

const readline = require("readline");

Sau đó, bạn cần tạo một phiên bản

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
9 được kết nối với luồng đầu vào. Bạn tạo
const input = prompt("What's your name?");
alert(`Your name is ${input}`);
9 bằng cách sử dụng phương thức
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
1, đồng thời chuyển các tùy chọn
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
2 và
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
3 làm đối số đối tượng

Vì bạn muốn đầu vào và đầu ra được ghi vào bàn điều khiển, bạn cần viết

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
2 là
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
5 và
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
3 là
const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
7

Đây là một ví dụ về việc tạo giao diện đường đọc

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

Để yêu cầu đầu vào của người dùng, bạn cần gọi phương thức

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
8 từ đối tượng
const input = prompt("What's your name?");
alert(`Your name is ${input}`);
9, được gán cho biến
const readline = require("readline");
0 trong đoạn mã trên

Phương thức

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
8 nhận hai tham số

  • const input = prompt("What's your name?");
    alert(`Your name is ${input}`);
    
    3 câu hỏi bạn muốn hỏi người dùng của mình
  • Đối tượng
    const readline = require("readline");
    
    3 (tùy chọn) nơi bạn có thể truyền tín hiệu
    const readline = require("readline");
    
    4
  • Hàm
    const readline = require("readline");
    
    5 để thực thi khi nhận được câu trả lời, chuyển
    const readline = require("readline");
    
    6 cho hàm

Bạn có thể bỏ qua đối tượng

const readline = require("readline");
3 và chuyển hàm
const readline = require("readline");
5 làm tham số thứ hai

Đây là cách bạn sử dụng phương pháp

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);
8

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("What is your name? ", function (answer) {
  console.log(`Oh, so your name is ${answer}`);
});

Cuối cùng, bạn có thể đóng giao diện

const readline = require("readline");
0 bằng cách gọi phương thức
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
1 bên trong hàm gọi lại

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("What is your name? ", function (answer) {
  console.log(`Oh, so your name is ${answer}`);
  console.log("Closing the interface");
  rl.close();
});

Lưu tệp dưới dạng

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
2, sau đó gọi tập lệnh bằng NodeJS như thế này

$ node ask.js
What is your name? Nathan
Oh, so your name is Nathan
Closing the interface
$

Và đó là cách bạn có thể yêu cầu đầu vào của người dùng bằng mô-đun NodeJS

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
7

Bạn cũng có thể sử dụng

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
4 từ NodeJS để thêm bộ đếm thời gian cho câu hỏi của mình và hủy nó khi một khoảng thời gian nhất định đã trôi qua

Nhưng xin lưu ý rằng phương pháp

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
4 chỉ khả dụng cho NodeJS phiên bản 15 trở lên. Và thậm chí sau đó, phương pháp này vẫn còn thử nghiệm

Câu hỏi sau sẽ bị hủy bỏ khi không có câu trả lời nào được đưa ra trong 10 giây sau lời nhắc. Mã này đã được thử nghiệm để hoạt động trên NodeJS phiên bản 16. 3. 0 trở lên

const readline = require("readline");
const ac = new AbortController();
const signal = ac.signal;

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("What is your name? ", { signal }, (answer) => {
  console.log(`Oh, so your name is ${answer}`);
  console.log("Closing the console");
  process.exit();
});

signal.addEventListener(
  "abort",
  () => {
    console.log("The name question timed out!");
  },
  { once: true }
);

setTimeout(() => {
  ac.abort();
  process.exit();
}, 10000); // 10 seconds

Bạn có thể thêm bộ đếm thời gian như trong đoạn mã trên cho các câu hỏi nhạy cảm về thời gian

Như bạn có thể thấy từ ví dụ trên, mô-đun

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
7 khá phức tạp so với phương thức
const input = prompt("What's your name?");
alert(`Your name is ${input}`);
1 dễ dàng từ trình duyệt

Ngoài ra, bạn có thể sử dụng mô-đun npm

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
8 để yêu cầu đầu vào của người dùng mà không cần sử dụng mô-đun
const input = prompt("What's your name?");
alert(`Your name is ${input}`);
7

Nhận đầu vào của người dùng từ NodeJS bằng mô-đun đồng bộ hóa nhắc nhở

Trước tiên, bạn cần cài đặt mô-đun

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
8 bằng npm hoặc Yarn như sau

npm install prompt-sync
# or
yarn add prompt-sync

Sau đó, bạn chỉ cần

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
8 mô-đun
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
8 và sử dụng phương thức
const input = prompt("What's your name?");
alert(`Your name is ${input}`);
1 như trong trình duyệt

Hãy nhìn vào đoạn mã dưới đây

const input = prompt("What's your name?");
alert(`Your name is ${input}`);
0

Vì phương thức này là đồng bộ, phiên bản Node của bạn sẽ đợi đầu vào trước khi thực hiện dòng tiếp theo. Để biết thêm thông tin, bạn có thể truy cập vào

Làm cách nào để lấy đầu vào trong NodeJS từ bảng điều khiển?

Tất cả đầu vào của người dùng sẽ được đọc dưới dạng Chuỗi, vì vậy để coi đầu vào của người dùng là số, bạn cần chuyển đổi đầu vào. .
const num = prompt('Nhập số. ');
bảng điều khiển. log('Số của bạn + 4 =');
bảng điều khiển. nhật ký (Số (số) + 4);

Làm cách nào để lấy đầu vào trong js từ bảng điều khiển?

Trong bảng điều khiển JavaScript, thông tin nhập của người dùng có thể được tạo bằng cách sử dụng phương thức “prompt()” . Cách tiếp cận này hiển thị một hộp thoại yêu cầu đầu vào từ người dùng. Nếu người dùng nhấp vào “OK”, nó sẽ trả về giá trị đầu vào, nếu không, nó sẽ trả về null. Nó chấp nhận “văn bản” như một tham số sẽ được hiển thị trong hộp thoại.

Làm cách nào để lấy đầu vào động trong NodeJS?

Bạn cần sử dụng formData để gửi phần tử được thêm động đến nút phụ trợ của bạn. js . Chúng ta cần sử dụng jQuery $. từng chức năng để nhận tất cả các giá trị của đầu vào mà bạn sẽ thêm vào.

Làm cách nào để lấy đầu vào mảng trong NodeJS?

javascript. Để sử dụng trong Codeforces . var num = readline(). tách(" "). map(x => parseInt(x)); // num sẽ là một mảng [1,2,3] var x = num[0] . var y= số[1]; .