What is Node.js

Node.js is a way to run JavaScript code not only in the browser. The applications that are most often created on this platform can be divided into two categories:

auxiliary tools for client applications — build systems, package distribution, development servers, code editors and static analysis systems;

server applications — ranging from simple APIs to huge corporate programs.

How to use Node.js

Section of the article “How to use”

Install Node.js to your computer. Now you can run a command in the terminal that will output the current version of the platform and prove that everything is installed correctly:

node -v

Copy

Node.js allows you to execute any JavaScript file. Let’s create a demo file script.js:

const whereIAm = __dirname

// __dirname is a global variable that stores the folder where the script file is located

console.log(`I am at ${whereIAm}`)

Copy

Let’s run it in the terminal:

node script.js

Copy

How to understand

Section of the article “How to understand”

The V8 engine developed by Google is responsible for executing JavaScript in the Google Chrome browser (and other browsers based on Chromium — Edge, Yandex.Browser, Opera). In 2009, Ryan Dahl, as an experiment, created the Node platform.js, which uses V8 to execute JavaScript code outside the browser.

In the Node environment.js the application does not have access to the standard browser API. For example, document and window are unavailable because the code is executed outside the browser. There are no documents and windows in its context.

The absolute majority of JavaScript development tools use Node.js . Many server applications also use it, mainly for server rendering of client applications and creating API gateways.

API gateway is a service with a client-friendly API that does not do any work itself, but only calls other services to receive data or perform an operation. The value of such services is in the API that is convenient for the client code.

For example, you can make a simple application on Node.a js that responds to all requests with the same text. First of all, this will require a third-party npm package:

npm init // initialize package.json

npm install express // adding a package for creating web applications

Copy

Now you need to create a file with the application code, let’s call it server.js:

// express — framework for creating web applications

const express = require(‘express’)

// creating an express application

const app = express()

// it will run on a specific computer port

const port = 3000

// if you send a GET request to /, we will get the response ‘Hello World!’

app.get(‘/’, (req, res) => {

  res.send(‘Hello World!’)

})

// launching

the app.listen(port, () => {

console.log(`The application is running on http://localhost :${port}`)

})

Copy

Everything is ready to launch! Execute the file with the command:

node server.js

Copy

If in the browser go to http://localhost:3000 , then you can see the response of the web server.

“Features of versioning”

Versions in Node.js obey some rules depending on the major version. This is the first number from the full version. For example, for Node.js 16.13.1 major version will be 16.

Odd versions (13, 15, etc.) are experimental. As a rule, the newest features appear in them, but the support period for such versions is only six months. This is important because critical errors or dangerous vulnerabilities are constantly being identified in programs.

Even-numbered versions (14, 16, etc.) after six months of development move to the LTS stage (from English long-term support, long-term support). This means that active development will be carried out within 12 months, new features will be added and bugs will be fixed. At the end of these 12 months, critical errors and security issues will be fixed for another 18 months.

Thus, it is better not to use odd versions, except when you need to try new features. In production, it is necessary to use LTS versions — they are the most stable and guarantee that critical vulnerabilities will be fixed for 30 months.

JSON

Briefly

JSON is the most popular format for data exchange between applications. This format is very similar to JavaScript objects. Objects are easily transformed into JSON for sending to the server.

How to spell

Section of the article “How to spell”

{

“brand”: “Apple”,

  “model”: “iPhone 11 Pro”,

  “isAvailable”: true,

  “display”: 5.8,

  “memories”: [64, 256, 512],

  “features”: {

    “tripleCamera”: true,

    “faceId”: true,

    “touchId”: false,

    “eSIM”: true

  }

}

JSON consists of key-value pairs. The pairs are separated by commas — ,, and the key is separated from the value by a colon — :. The key can only be a string wrapped in double quotes. But the meaning is almost anything:

The string in double quotes is “I love JSON!”;

Number — 21;

The boolean value is true;

Array — [18, true, “lost”, [4, 8, 15, 16, 23, 42]];

The object is {“isValid”: true, “isPayed”: false}.

JSON is based on JavaScript, but is a language-independent specification for data and can be used with almost any programming language, so it skips some specific JavaScript object values.:

Object methods (functions) — {greetings() {alert(“Hello World!”)}};

Keys with the undefined value are {“value”: undefined}.

If you need to save JSON to a file, then use the extension.json.

How to understand

Section of the article “How to understand”

JSON is used to get data from the server. Typical work scheme:

Sending a request to the server;

We are waiting for an answer;

Getting a JSON with a data set;

Turning JSON into a JavaScript object;

We use the data.

Example:

Conversion to JSON

In order to turn data into JSON code, use the JSON.stringify() method. The first argument of the method takes the value to be converted.

Converting a JavaScript object to JSON:

JSON is very convenient to use to get data on the network. For example, one of the popular weather forecast services, Open Weather, can send data to JSON via the API. Here is a JSON with the weather in London.

JSON is supported by most programming languages, so it is convenient to store service information and settings in it.

🛠 JSON does not support comments, JavaScript comment // comment will result in an error.

🛠 Alternative data transmission formats are XML and YAML.

The most famous JSON file is the configuration file of the package manager npm – package.json.

Leave a Reply

Your email address will not be published. Required fields are marked *