Replace Node.JS with Node.js

This commit is contained in:
Alexey Pyltsyn 2019-04-23 21:29:04 +03:00
parent b9ca311f19
commit 735880d45f
12 changed files with 17 additions and 17 deletions

View file

@ -45,7 +45,7 @@ The engine applies optimizations at each step of the process. It even watches th
Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it. Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
JavaScript's capabilities greatly depend on the environment it's running in. For instance, [Node.JS](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc. JavaScript's capabilities greatly depend on the environment it's running in. For instance, [Node.js](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.
In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver. In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver.

View file

@ -1,10 +1,10 @@
# Hello, world! # Hello, world!
The tutorial that you're reading is about core JavaScript, which is platform-independent. Later on, you'll learn about Node.JS and other platforms that use it. The tutorial that you're reading is about core JavaScript, which is platform-independent. Later on, you'll learn about Node.js and other platforms that use it.
But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We'll keep the amount of browser-specific commands (like `alert`) to a minimum so that you don't spend time on them if you plan to concentrate on another environment (like Node.JS). We'll focus on JavaScript in the browser in the [next part](/ui) of the tutorial. But we need a working environment to run our scripts and, since this book is online, the browser is a good choice. We'll keep the amount of browser-specific commands (like `alert`) to a minimum so that you don't spend time on them if you plan to concentrate on another environment (like Node.js). We'll focus on JavaScript in the browser in the [next part](/ui) of the tutorial.
So first, let's see how we attach a script to a webpage. For server-side environments (like Node.JS), you can execute the script with a command like `"node my.js"`. So first, let's see how we attach a script to a webpage. For server-side environments (like Node.js), you can execute the script with a command like `"node my.js"`.
## The "script" tag ## The "script" tag

View file

@ -287,7 +287,7 @@ Most linters are integrated with many popular editors: just enable the plugin in
For instance, for ESLint you should do the following: For instance, for ESLint you should do the following:
1. Install [Node.JS](https://nodejs.org/). 1. Install [Node.js](https://nodejs.org/).
2. Install ESLint with the command `npm install -g eslint` (npm is a JavaScript package installer). 2. Install ESLint with the command `npm install -g eslint` (npm is a JavaScript package installer).
3. Create a config file named `.eslintrc` in the root of your JavaScript project (in the folder that contains all your files). 3. Create a config file named `.eslintrc` in the root of your JavaScript project (in the folder that contains all your files).
4. Install/enable the plugin for your editor that integrates with ESLint. The majority of editors have one. 4. Install/enable the plugin for your editor that integrates with ESLint. The majority of editors have one.

View file

@ -424,4 +424,4 @@ alert(`Loading started ${performance.now()}ms ago`);
// more than 3 digits after the decimal point are precision errors, but only the first 3 are correct // more than 3 digits after the decimal point are precision errors, but only the first 3 are correct
``` ```
Node.JS has `microtime` module and other ways. Technically, any device and environment allows to get more precision, it's just not in `Date`. Node.js has `microtime` module and other ways. Technically, any device and environment allows to get more precision, it's just not in `Date`.

View file

@ -3,7 +3,7 @@
The global object provides variables and functions that are available anywhere. Mostly, the ones that are built into the language or the host environment. The global object provides variables and functions that are available anywhere. Mostly, the ones that are built into the language or the host environment.
In a browser it is named "window", for Node.JS it is "global", for other environments it may have another name. In a browser it is named "window", for Node.js it is "global", for other environments it may have another name.
For instance, we can call `alert` as a method of `window`: For instance, we can call `alert` as a method of `window`:

View file

@ -7,7 +7,7 @@ There are two methods for it:
- `setTimeout` allows to run a function once after the interval of time. - `setTimeout` allows to run a function once after the interval of time.
- `setInterval` allows to run a function regularly with the interval between the runs. - `setInterval` allows to run a function regularly with the interval between the runs.
These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.JS. These methods are not a part of JavaScript specification. But most environments have the internal scheduler and provide these methods. In particular, they are supported in all browsers and Node.js.
## setTimeout ## setTimeout
@ -99,7 +99,7 @@ clearTimeout(timerId);
alert(timerId); // same identifier (doesn't become null after canceling) alert(timerId); // same identifier (doesn't become null after canceling)
``` ```
As we can see from `alert` output, in a browser the timer identifier is a number. In other environments, this can be something else. For instance, Node.JS returns a timer object with additional methods. As we can see from `alert` output, in a browser the timer identifier is a number. In other environments, this can be something else. For instance, Node.js returns a timer object with additional methods.
Again, there is no universal specification for these methods, so that's fine. Again, there is no universal specification for these methods, so that's fine.
@ -382,7 +382,7 @@ First timers run immediately (just as written in the spec), and then the delay c
That limitation comes from ancient times and many scripts rely on it, so it exists for historical reasons. That limitation comes from ancient times and many scripts rely on it, so it exists for historical reasons.
For server-side JavaScript, that limitation does not exist, and there exist other ways to schedule an immediate asynchronous job, like [process.nextTick](https://nodejs.org/api/process.html) and [setImmediate](https://nodejs.org/api/timers.html) for Node.JS. So the notion is browser-specific only. For server-side JavaScript, that limitation does not exist, and there exist other ways to schedule an immediate asynchronous job, like [process.nextTick](https://nodejs.org/api/process.html) and [setImmediate](https://nodejs.org/api/timers.html) for Node.js. So the notion is browser-specific only.
```` ````
### Allowing the browser to render ### Allowing the browser to render

View file

@ -37,7 +37,7 @@ let f = user.sayHi;
setTimeout(f, 1000); // lost user context setTimeout(f, 1000); // lost user context
``` ```
The method `setTimeout` in-browser is a little special: it sets `this=window` for the function call (for Node.JS, `this` becomes the timer object, but doesn't really matter here). So for `this.firstName` it tries to get `window.firstName`, which does not exist. In other similar cases as we'll see, usually `this` just becomes `undefined`. The method `setTimeout` in-browser is a little special: it sets `this=window` for the function call (for Node.js, `this` becomes the timer object, but doesn't really matter here). So for `this.firstName` it tries to get `window.firstName`, which does not exist. In other similar cases as we'll see, usually `this` just becomes `undefined`.
The task is quite typical -- we want to pass an object method somewhere else (here -- to the scheduler) where it will be called. How to make sure that it will be called in the right context? The task is quite typical -- we want to pass an object method somewhere else (here -- to the scheduler) where it will be called. How to make sure that it will be called in the right context?

View file

@ -590,7 +590,7 @@ Let's imagine we've got a fatal error outside of `try..catch`, and the script di
Is there a way to react on such occurrences? We may want to log the error, show something to the user (normally they don't see error messages) etc. Is there a way to react on such occurrences? We may want to log the error, show something to the user (normally they don't see error messages) etc.
There is none in the specification, but environments usually provide it, because it's really useful. For instance, Node.JS has [process.on('uncaughtException')](https://nodejs.org/api/process.html#process_event_uncaughtexception) for that. And in the browser we can assign a function to special [window.onerror](mdn:api/GlobalEventHandlers/onerror) property. It will run in case of an uncaught error. There is none in the specification, but environments usually provide it, because it's really useful. For instance, Node.js has [process.on('uncaughtException')](https://nodejs.org/api/process.html#process_event_uncaughtexception) for that. And in the browser we can assign a function to special [window.onerror](mdn:api/GlobalEventHandlers/onerror) property. It will run in case of an uncaught error.
The syntax: The syntax:

View file

@ -292,7 +292,7 @@ If an error occurs, and there's no `.catch`, the `unhandledrejection` handler tr
Usually such errors are unrecoverable, so our best way out is to inform the user about the problem and probably report the incident to the server. Usually such errors are unrecoverable, so our best way out is to inform the user about the problem and probably report the incident to the server.
In non-browser environments like Node.JS there are other similar ways to track unhandled errors. In non-browser environments like Node.js there are other similar ways to track unhandled errors.
## Summary ## Summary

View file

@ -11,7 +11,7 @@ But eventually scripts became more and more complex, so the community invented a
For instance: For instance:
- [AMD](https://en.wikipedia.org/wiki/Asynchronous_module_definition) -- one of the most ancient module systems, initially implemented by the library [require.js](http://requirejs.org/). - [AMD](https://en.wikipedia.org/wiki/Asynchronous_module_definition) -- one of the most ancient module systems, initially implemented by the library [require.js](http://requirejs.org/).
- [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1) -- the module system created for Node.JS server. - [CommonJS](http://wiki.commonjs.org/wiki/Modules/1.1) -- the module system created for Node.js server.
- [UMD](https://github.com/umdjs/umd) -- one more module system, suggested as a universal one, compatible with AMD and CommonJS. - [UMD](https://github.com/umdjs/umd) -- one more module system, suggested as a universal one, compatible with AMD and CommonJS.
Now all these slowly become a part of history, but we still can find them in old scripts. The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js. Now all these slowly become a part of history, but we still can find them in old scripts. The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js.

View file

@ -4,7 +4,7 @@ The JavaScript language was initially created for web browsers. Since then, it h
A platform may be a browser, or a web-server, or a washing machine, or another *host*. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*. A platform may be a browser, or a web-server, or a washing machine, or another *host*. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
A host environment provides platform-specific objects and functions additional to the language core. Web browsers give a means to control web pages. Node.JS provides server-side features, and so on. A host environment provides platform-specific objects and functions additional to the language core. Web browsers give a means to control web pages. Node.js provides server-side features, and so on.
Here's a bird's-eye view of what we have when JavaScript runs in a web-browser: Here's a bird's-eye view of what we have when JavaScript runs in a web-browser:

View file

@ -274,7 +274,7 @@ To get connection state, additionally there's `socket.readyState` property with
## Chat example ## Chat example
Let's review a chat example using browser WebSocket API and Node.JS WebSocket module <https://github.com/websockets/ws>. Let's review a chat example using browser WebSocket API and Node.js WebSocket module <https://github.com/websockets/ws>.
HTML: there's a `<form>` to send messages and a `<div>` for incoming messages: HTML: there's a `<form>` to send messages and a `<div>` for incoming messages:
@ -314,7 +314,7 @@ socket.onmessage = function(event) {
Server-side code is a little bit beyound our scope here. We're using browser WebSocket API, a server may have another library. Server-side code is a little bit beyound our scope here. We're using browser WebSocket API, a server may have another library.
Still it can also be pretty simple. We'll use Node.JS with <https://github.com/websockets/ws> module for websockets. Still it can also be pretty simple. We'll use Node.js with <https://github.com/websockets/ws> module for websockets.
The algorithm will be: The algorithm will be:
1. Create `clients = new Set()` -- a set of sockets. 1. Create `clients = new Set()` -- a set of sockets.