Skip to content

Commit

Permalink
chore: run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
haslie22 committed Nov 17, 2023
1 parent 55b198a commit 48f5a93
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 61 deletions.
8 changes: 4 additions & 4 deletions stage1/modules/node-materials/node/module/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fs.writeFile(
(err) => {
if (err) throw err;
console.log("File was created");
},
}
);
```

Expand All @@ -57,7 +57,7 @@ fs.appendFile(
(err) => {
if (err) throw err;
console.log("File was modified");
},
}
);
```

Expand All @@ -73,7 +73,7 @@ fs.readFile(
(err, data) => {
if (err) throw err;
console.log(data);
},
}
);
```

Expand All @@ -89,7 +89,7 @@ fs.rename(
(err) => {
if (err) throw err;
console.log("File renamed");
},
}
);
```

Expand Down
6 changes: 3 additions & 3 deletions stage1/modules/node-materials/node/module/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ This method takes a callback function `requestHandler` with two parameters, `req
- `request` holds information about the request
- `response` is responsible for sending the response

Our `requestHandler` logs the request method and the address of the requested resource to the console. It also sends the messages `Hello from Node.js` and `Bye!` as responses.
The `response.write()` method writes the message to the response body, and `response.end()` informs the server that the headers and body of the response are written and it can be sent.
Our `requestHandler` logs the request method and the address of the requested resource to the console. It also sends the messages `Hello from Node.js` and `Bye!` as responses.
The `response.write()` method writes the message to the response body, and `response.end()` informs the server that the headers and body of the response are written and it can be sent.
Note that `response.end()` should terminate every response. Without it, the request processing will "hang" — the request will be received but not fully processed.

```js
Expand All @@ -69,7 +69,7 @@ server.listen(PORT, "localhost", () => {
});
```

Run the file with the code, open your browser, and go to the address `localhost:3000/some/page`.
Run the file with the code, open your browser, and go to the address `localhost:3000/some/page`.
Note that to run the server with different code, you need to stop it and restart it. You already know how to terminate a Node.js process. Only one server can be running on a port at a time.

In the `write` and `end` methods, you can pass a string containing HTML tags with inline styles. These tags will be correctly processed by the browser.
Expand Down
10 changes: 5 additions & 5 deletions stage1/modules/node-materials/node/node-argv.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In Node.js, you can run a file with specific command line arguments. When runnin
node test 1 2 3
```

`1`, `2`, `3` are the arguments.
`1`, `2`, `3` are the arguments.
How can you access the arguments passed when the file is launched within the code? This is done using the `process.argv` property of the global `process` object.

In the `test.js` file, let's write the code:
Expand All @@ -17,7 +17,7 @@ In the `test.js` file, let's write the code:
console.log(process.argv);
```

In the terminal, execute the command `node test 1 2 3`.
In the terminal, execute the command `node test 1 2 3`.
In the console, an array is displayed, the first two elements of which are the path to the node.exe file and the path to the executed file. After that come the passed arguments.

If you only need to get the arguments, execute the code:
Expand Down Expand Up @@ -59,7 +59,7 @@ console.log(message);

### Practical Usage

In practice, when you write code to handle command line arguments yourself, it is necessary to handle various situations correctly — an argument may be missing, a flag may not be passed, or it may be passed without a value. The mere presence of a flag can also be a boolean value, and so on.
In practice, when you write code to handle command line arguments yourself, it is necessary to handle various situations correctly — an argument may be missing, a flag may not be passed, or it may be passed without a value. The mere presence of a flag can also be a boolean value, and so on.
To make working with command line arguments more convenient and minimize the likelihood of errors, it is useful to use ready-made solutions such as [minimist](https://www.npmjs.com/package/minimist), [commander](https://www.npmjs.com/package/commander), [yargs](https://www.npmjs.com/package/yargs), and others.

### CLI Options
Expand Down Expand Up @@ -88,7 +88,7 @@ will result in an error. You can view the full list of options in the [documenta

### Environment Variables

Sometimes we need to pass some value from the **outside** to our code, which will be used by our application. For example, we want to implement different behavior when the application is launched on a production server and during development. Environment variables can help us in this case.
Sometimes we need to pass some value from the **outside** to our code, which will be used by our application. For example, we want to implement different behavior when the application is launched on a production server and during development. Environment variables can help us in this case.

Environment variables have a syntax like `variable_name=variable_value` and are placed before `node ....`. In Bash, for example, you can pass a variable that shows in which mode the application is currently running:

Expand Down Expand Up @@ -150,7 +150,7 @@ stdin.on("data", (data) => {
const numStringsArray = numString.split(" ");
const hasIncorrectLength = numStringsArray.length !== 2;
const hasIncorrectValues = numStringsArray.some((numStr) =>
Number.isNaN(+numStr),
Number.isNaN(+numStr)
);
if (hasIncorrectLength || hasIncorrectValues) {
stdout.write("You need to enter 2 numbers separated by a space");
Expand Down
6 changes: 3 additions & 3 deletions stage1/modules/node-materials/node/node-fs-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

[HOME](../README.md)

Unlike browser-based JavaScript, Node.js has access to the file system.
Unlike browser-based JavaScript, Node.js has access to the file system.
For example, we can easily determine the absolute path to the directory containing our file. To do this, open the `test.js` file and write the code:

```js
console.log(__dirname);
console.log(__dirname);
```

Open the terminal and run the file:
Expand All @@ -15,7 +15,7 @@ Open the terminal and run the file:
node test
```

The console will output the absolute path to the directory with the `test.js` file.
The console will output the absolute path to the directory with the `test.js` file.
Now let's output the absolute path to the file. To do this, add the line in the `test.js` file:

```js
Expand Down
16 changes: 8 additions & 8 deletions stage1/modules/node-materials/node/node-introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ Download link https://nodejs.org/en

Download and install the latest LTS version (Recommended For Most Users)

Check what you have installed. To do this, check the version of Node.js.
Check what you have installed. To do this, check the version of Node.js.
Open Git Bash: right-click on the desktop, choose `Git Bash Here` from the context menu. If this option is not available, download and install Git https://git-scm.com/downloads.

Execute the command `node -v`.
Execute the command `node -v`.
If the Node.js version is displayed, then we have successfully completed the first step, and Node.js is installed.

### Possible Installation Issues

Sometimes, after installing Node.js, commands in the terminal starting with `node` may cause errors like `'node' is not recognized as an internal or external command, operable program or batch file`.
In this case, you need to add the correct path to the Node.js directory to the `PATH`.
Sometimes, after installing Node.js, commands in the terminal starting with `node` may cause errors like `'node' is not recognized as an internal or external command, operable program or batch file`.
In this case, you need to add the correct path to the Node.js directory to the `PATH`.
[Example solution](https://love2dev.com/blog/node-is-not-recognized-as-an-internal-or-external-command/).

### Where to Write Code
Expand All @@ -26,10 +26,10 @@ Neither writing code in the terminal, nor is Notepad is very convenient. Let's c

### REPL Mode

You can write and execute code directly in the terminal.
You can write and execute code directly in the terminal.
This mode is called `REPL` (Read-Eval-Print-Loop).

To enter it, execute the command `node` in the terminal.
To enter it, execute the command `node` in the terminal.
Now you can write code directly in the terminal, and REPL will evaluate the entered expression and display the result. For example, if you enter `2 + 2` and press Enter, REPL will output `4`.

You can also make explicit console output using the familiar methods. For example, you can write:
Expand All @@ -38,8 +38,8 @@ You can also make explicit console output using the familiar methods. For exampl
console.log("Hello, world!");
```

REPL has some useful commands, information about which can be obtained by sending the `.help` command.
To exit REPL mode, send the `.exit` command (also, for a more forceful process termination, you can use the standard key combination for the terminal used, such as `Ctrl + C`).
REPL has some useful commands, information about which can be obtained by sending the `.help` command.
To exit REPL mode, send the `.exit` command (also, for a more forceful process termination, you can use the standard key combination for the terminal used, such as `Ctrl + C`).
Clear the terminal using commands like `cls` (for the standard Windows Command Prompt and PowerShell) or `clear` (for Bash).

### How to Run a File
Expand Down
2 changes: 1 addition & 1 deletion stage1/modules/node-materials/node/node-io.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const users = connection.query("SELECT * FROM users"); // make a query
console.log(users); // display information in the console
```

This is a simple implementation but a very time-consuming one: the program waits for **250 million processor cycles** until a connection to the database occurs.
This is a simple implementation but a very time-consuming one: the program waits for **250 million processor cycles** until a connection to the database occurs.
The second problem with synchronous I/O is that it is not fault-tolerant. If the program cannot connect to the database or if the requested information is not found in the database, the program will stop working.

Synchronous or blocking I/O in Node.js is used very rarely:
Expand Down
12 changes: 6 additions & 6 deletions stage1/modules/node-materials/node/node-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Advantages of using modules in Node.js:

To simplify, modules in Node.js can be divided into three types:

1. Core modules, which we get "out of the box" by installing Node.js on a computer.
Examples of core modules:
1. Core modules, which we get "out of the box" by installing Node.js on a computer.
Examples of core modules:

- [path module](module/path.md)
- [fs module](module/fs.md)
Expand All @@ -29,12 +29,12 @@ Examples of core modules:

### Core Modules

They are already compiled into binary code and described in the documentation.
They are already compiled into binary code and described in the documentation.
[List of core modules](https://nodejs.org/dist/latest-v14.x/docs/api/)

Core modules are easy to include, and you can start working with them.

To include a module, the `require()` function is used.
To include a module, the `require()` function is used.
Examples of module inclusion:

```js
Expand Down Expand Up @@ -71,7 +71,7 @@ Let's create a new project. To do this, create a project folder, open it in VS C
npm init -y
```

The `-y` (Yes) parameter means that we agree with all project settings by default.
The `-y` (Yes) parameter means that we agree with all project settings by default.
A `package.json` file is created in the project folder, describing the created application.

### Installing Packages via npm
Expand All @@ -98,5 +98,5 @@ Installed modules are added to the `node_modules` folder, and information about

If you delete the `node_modules` folder and execute the `npm install` command, the `node_modules` folder will be restored along with all the added modules based on the records in the `package.json` file.

Node.js projects are added to GitHub without the `node_modules` folder but with the `package.json` and `package-lock.json` files.
Node.js projects are added to GitHub without the `node_modules` folder but with the `package.json` and `package-lock.json` files.
After downloading such a project, you need to execute the `npm install` command in the terminal to restore all modules installed through npm.
8 changes: 4 additions & 4 deletions stage1/modules/node-materials/node/node-stdio.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const { stdin, stdout } = process;
stdin.on("data", (data) => stdout.write(data));
```

Using the `.on()` method, we subscribe to the `data` event of the `stdin` object.
Using the `.on()` method, we subscribe to the `data` event of the `stdin` object.
The `.on()` method takes two parameters - the event name `data` and the arrow function handler `data => stdout.write(data)`, which outputs the entered data to the console.

Now, when we enter some text in the console and press the Enter key, `stdout.write()` returns the text we entered.
Expand All @@ -45,8 +45,8 @@ The `process.exit()` method emits the `exit` event when executed, and by subscri
process.on("exit", () => stdout.write("Good luck learning Node.js!"));
```

`process.exit()` takes an optional argument `exitCode`, represented by an integer.
By default, this method is called with `exitCode === 0`. This type of process termination indicates that the program executed successfully and ran without errors.
`process.exit()` takes an optional argument `exitCode`, represented by an integer.
By default, this method is called with `exitCode === 0`. This type of process termination indicates that the program executed successfully and ran without errors.
Exiting the process with any other `exitCode` signifies that the program terminated with an error. This allows different messages to be passed depending on whether the program executed as intended or not:

```js
Expand Down Expand Up @@ -98,7 +98,7 @@ stdin.on("data", (data) => {

If we log the type of the `data` variable to the console, we will see `object`. By applying the [trick with the special `[[Class]]` property](https://learn.javascript.ru/class-instanceof#sekretnoe-svoystvo-class), we get `[object Uint8Array]` for `data`.

Since `process.stdin` is a stream, it works with data in **binary** format. To handle such data in Node.js, there is a special `Buffer` object, which is a subclass of `Uint8Array` (a typed array storing 8-bit unsigned integer values).
Since `process.stdin` is a stream, it works with data in **binary** format. To handle such data in Node.js, there is a special `Buffer` object, which is a subclass of `Uint8Array` (a typed array storing 8-bit unsigned integer values).
The data contained in the `Buffer` can be converted to a string:

```js
Expand Down
28 changes: 14 additions & 14 deletions stage1/modules/node-materials/node/projects/github-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Let's create a console application that, as a command-line argument, will take a

To achieve this, we'll use the GitHub API. The request `https://api.github.com/users/USERNAME/repos` returns a list of repositories for the user whose `USERNAME` is specified in the link.

1. Start by creating a new Node.js application
1. Start by creating a new Node.js application

Create a folder named github-app, open it in VS Code, and run the following command in the terminal:

Expand All @@ -19,7 +19,7 @@ Create two files:
- `app.js` - the main application file
- `github.js` - the file where we write the logic for interacting with the API

2. Let's start with the `app.js` file
2. Let's start with the `app.js` file

Import the `github` module into it. Since this is a manually created module, specify the path to the module code file as a parameter to the `require()` method:

Expand All @@ -33,7 +33,7 @@ Create a variable `username` to store the command-line argument passed when the
const username = process.argv[2];
```

Use the imported `github` object, which has a `getRepos()` property - a function that returns a list of user repositories.
Use the imported `github` object, which has a `getRepos()` property - a function that returns a list of user repositories.
The parameters of the `getRepos()` function are `username` - the username and a callback function taking two parameters: `error` - an error, and `repos` - the received data, in our case, a list of repositories.
In the callback function, let's handle the error and print the names of the repositories to the console:

Expand All @@ -45,9 +45,9 @@ github.getRepos(username, (error, repos) => {
});
```

3. Move on to the `github.js` file
3. Move on to the `github.js` file

Our application will communicate with the server over the `https` protocol. For this, Node.js has a built-in `https module`, similar to the [HTTP Module](../module/http.md).
Our application will communicate with the server over the `https` protocol. For this, Node.js has a built-in `https module`, similar to the [HTTP Module](../module/http.md).

To send a request to the API, we'll use the `get()` method, which allows us to retrieve data from the server.
Import the `https` module and write the code for the `getRepos()` function. The function parameter is `username` - the GitHub username.
Expand All @@ -72,8 +72,8 @@ Run the `app` file. Specify a well-known repository as a command-line argument w
node app goldbergyoni
```

The application returns a `403` error.
The response code for the error status `HTTP 403 Forbidden` indicates that the server understood the request but refuses to authorize it.
The application returns a `403` error.
The response code for the error status `HTTP 403 Forbidden` indicates that the server understood the request but refuses to authorize it.
However, if we try to request the same data from the browser by visiting the link https://api.github.com/users/goldbergyoni/repos, we get the page with the data we need.

To make a request to the API, we need to specify the `User-Agent` header. While the browser adds this header automatically when navigating to a link, in a Node.js application, we need to specify it.
Expand All @@ -92,9 +92,9 @@ const option = {

Use the `option` object as the first parameter in `the https.get` method. The application returns a `200` status, indicating a successful connection.

4. Handling incoming data
4. Handling incoming data

Almost everything in Node.js, including communication with the server, is implemented asynchronously, using events and streams. Information from the server comes in parts.
Almost everything in Node.js, including communication with the server, is implemented asynchronously, using events and streams. Information from the server comes in parts.
The server response (`res`) has a `data` event, which fires when a part of the requested information comes from the server. Subscribe to this event and print the received data to the console:

```js
Expand Down Expand Up @@ -133,7 +133,7 @@ function getRepos(username) {
}
```

The response (`res`) method has an `end` event, which triggers when the data transmission is complete.
The response (`res`) method has an `end` event, which triggers when the data transmission is complete.
Upon the occurrence of this event, use the `JSON.parse(body)` method to convert the received data into an array:

```js
Expand All @@ -157,7 +157,7 @@ function getRepos(username) {
}
```

5. Passing data to the `app` module
5. Passing data to the `app` module

To pass the obtained data to the `app` module, which imports the `github` module, note that the `app` module expects the `getRepos()` function with two parameters - `username` and a callback function that also has two parameters `(error, repos)`. This callback function needs to be specified as a parameter to the `getRepos()` function in the `github` module. Let's name it `done` - a standard name for such functions.

Expand Down Expand Up @@ -204,7 +204,7 @@ if (!username) return done(new Error("Username is required"));
Create a variable `request` and set its value to the `https.get()` method:

```js
req.on('error', error => done(new Error('Failed to send request')));
req.on("error", (error) => done(new Error("Failed to send request")));
```

3. An error in receiving a response from the server is indicated by a response status other than `200`:
Expand Down Expand Up @@ -253,8 +253,8 @@ function getRepos(username, done) {
} else {
done(
new Error(
`Error working with the server ${res.statusCode} ${res.statusMessage}`,
),
`Error working with the server ${res.statusCode} ${res.statusMessage}`
)
);
}
});
Expand Down
Loading

0 comments on commit 48f5a93

Please sign in to comment.