Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

What I got into or misunderstood when learning Node.js for the first time

I use Node.js, but my impression of Node.js was quite different before and after learning Node.js, so I have compiled a brief explanation of Node.js and a brief summary of what I personally got stuck in when learning it.

What is Node.js?

Node.js is a JavaScript execution environment.

JavaScript is generally run on a browser, but once Node.js is installed, the program can be run in JavaScript by itself, independent of the browser.

Chrome's JavaScript execution engine is open source, and Node.js takes that JavaScript execution engine and allows it to run on its own.

It allows access to OS functions outside the browser that are not possible with JavaScript run inside the browser, such as reading and writing PC local files, making JavaScript one of the languages that can be run on the PC.

For example, on a PC with php installed, a php program can be executed by typing $ php test.php, and on a PC with Node.js installed, a JavaScript program can be executed by typing $ node test.js.

test.js

console.log('Hello World!');

What kind of places is Node.js used?

Node.js is mainly used for

  • Use as a web server
  • Use as JavaScript development environment

In the use of the

Use as a web server

For example, here is the flow of how php works on a website.

  1. User accesses the website
  2. Web servers such as Nginx and Apache receive access
  3. The web server looks at the URL you accessed and decides which php program to run.
  4. The web server starts php and executes the above php program

With Node.js, on the other hand, the web server itself is created as a JavaScript program and executed by Node.js.

For example, a server program that displays "Hello World! If you create a program and type the command $ node test.js, Node.js will become the web server.

const http = require('http');
const port = 3000;

const server = http.createServer((request, response)=>{
    response.end('Hello World!');
});

server.listen(port);

structure

The last server.listen(port) creates a permanent loop that keeps checking for access to port 3000, so the program does not terminate and a web server is created that responds with "Hello World!

Node.js is just an execution environment for JavaScript, so "Node.js does not = web server", but it is listed here because it is often used as a web server.

Of course, it is also possible to write programs in a non-persistent, batch-processing manner, and in fact, this is often the case.

Why is Node.js attracting attention as a language to be used on servers?

Node.js is often noted as a program that runs on servers, including web server programs.

When building a web service, it is important for the server program to be able to handle a large number of accesses so that users do not have to wait for a large number of accesses.

Node.js is often used as a language to run on servers because it is more resistant to large amounts of access than other languages.

structure

Why is it that writing a program in Node.js is more resistant to access, even though it is the same PC?

That's because Node.js is

  • single program
  • asynchronous processing

This is because they are working on the basis of

single program

Typically, in other programming languages, a new program is executed each time a user accesses it.

Each time a new program is executed, CPU and memory are allocated for that program, so CPU and memory usage of the PC increases in proportion to the number of simultaneous accesses.

In Node.js, on the other hand, like the aforementioned web server program, the program is only the first one started, and that program waits for access from the user, and when the user accesses the program, the program handles it.

Therefore, even if the number of concurrent users increases, CPU and memory usage does not increase proportionally, and a large number of accesses can be handled.

asynchronous processing

In Node.js, most functions are executed asynchronously.

Asynchronous functions are, to put it bluntly, a way of writing functions in which, when executing a function, only the start of execution is performed first, and the actual processing is performed when the function is ready to be processed and the function is called later to perform the actual processing.

For example, suppose you have a function that accesses a website and retrieves information.

In other programming languages, when you execute a function, the function does not return until you access the site and get the information.

So if the site response is slow, executing the function will lock the program there for a long time.

In Node.js, on the other hand, when a function is executed, the function only registers that it wants to be told when the site's information has been obtained and then exits to the next process. Then, when it finishes getting the information, the function is called and the process after getting the information is executed.

The program will look like the following.

fetch('https://www.google.com')
    .then(res=>{
        return res.text();
    }).then(text=>{
        console.log(text);
    });

fetch('https://www.bing.com')
    .then(res=>{
        return res.text();
    }).then(text=>{
        console.log(text);
    });

fetch() is a function that accesses a URL and retrieves its contents, and console.log(text) is used to output the results.

The order of output is not necessarily "google -> bing", but rather the one with the fastest response is output first.

In other words, Node.js does not wait for something to be done, but rather puts everything to be done on a task list for now, and then executes the tasks as they become available.

Therefore, the lack of waiting time means that more can be done per hour and more access can be handled.

Use as JavaScript development environment

Before the advent of Node.js, JavaScript was just one of the site's resources, such as html, images, and css.

However, with the advent of Node.js, PC programs can now be written in JavaScript, which is familiar to web developers, and website development environments and other applications and tools are also being built in JavaScript.

Even when writing JavaScript to be executed in a browser, more and more people are first installing Node.js on their local PCs and then using various tools and applications related to website creation written in Node.js to create the JavaScript for their sites.

In other words, initially most programs were for use on servers, but now the language is increasingly used to run applications for use on local PCs.

Other Comments, etc.

Personally, I had a hard time getting to grips with writing asynchronous functions.

Unlike programs with synchronous functions, programs are not executed from the top down, and to understand what happens when requires some knowledge of the internal structure of Node.js, so learning Node.js and asynchronous functions is necessary in addition to learning JavaScript simply as a language.

Also, programs that do the same thing are harder to write and read than other programming languages because of asynchronous functions.

For example, a program written in Python and JavaScript that displays "Hello" and "World!" with a space of one second between each would look like the following.

import time

time.sleep(1)
print('Hello')

time.sleep(1)
print('World!')
setTimeout(()=>{
    console.log('Hello');

    setTimeout(()=>{
        console.log('World!');

    , 1000);
, 1000);

However, there are great advantages to using Node.js (JavaScript), so it is worth learning.

  • Lightweight (in terms of resistance to lots of access)
  • Wide range of applications

    • Browser, server, and local programs can all be written.
    • Most libraries are available and there are many examples (anything can be done and there is a lot of information for that)

summary

  • It's JavaScript made to work on its own language. It has nothing to do with the browser.
  • It's strong for lots of access. So, it is a language for web services and server side.
  • It can be used as a normal programming language outside of the server side. In fact, it is being used more and more outside of the server side.
  • The way the program is written is difficult to understand from the perspective of other programming languages.

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com