Toto je starší verze dokumentu!
Úkoly
* Seznamte se s nodejs, ověřte že máte nainstalovanou rozumně novou verzi a zkuste interakci s javascriptem skrze NodeJS terminál. * Napište hello world který načte nějaký text ze standardního vstupu (například uživatelovo jméno „Adam“) a na standardní výstup vypíše „Ahoj Adam“. * Vytvořte si ve svém oblíbeném IDE (např. VS Code) javascript projekt. Seznamte se s package.json souborem a zkuste přidat nějaký balíček jako závislost pomocí npm. Projekt verzujte pomocí GITu.
NodeJS CLI program
Using node18, how to write the simplest command line program in javascript, that uses stdin and stdout?
ChatGPT advice
To create a simple command-line program in Node.js that uses stdin and stdout, you can follow these steps:
- Make sure you have Node.js installed on your system.
- Create a new directory for your project and navigate to it in your terminal.
- Create a JavaScript file for your program. Let's call it simple-cli.js.
- In simple-cli.js, you can use the process.stdin and process.stdout streams to read from stdin and write to stdout. Here's a basic example that reads input from stdin and writes it back to stdout:
// Import the 'readline' module to handle input and output const readline = require('readline'); // Create an interface for reading from stdin and writing to stdout const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Prompt the user for input rl.question('Please enter something: ', (input) => { // Process the input const output = `You entered: ${input}`; // Write the output to stdout console.log(output); // Close the readline interface rl.close(); });
Save the file and run your program from the command line:
node simple-cli.js
That's it! You've created a simple command-line program in JavaScript using stdin and stdout. You can customize and expand upon this basic template to perform more complex tasks as needed.