3. Crazy geek


The original:
https://medium.freecodecamp.o…


This article first send WeChat messages public number: front-end pioneer welcome attention, every day to you push fresh front-end technology articles


Node.js is a JavaScript runtime environment. That sounds great, but what does it really mean? How does it work?

The Node runtime environment contains everything you need to execute JavaScript programs.

If you know anything about Java, they’re kind of similar.

JavaScript used to run only in a browser, but when it was extended to run as a standalone program on your computer, Node.js came along.

Now you can do a lot more with JavaScript than just interact with websites and special effects.

JavaScript can now do things that other scripting languages, such as Python, can do.

JavaScript and Node.js in your Chrome browser both run on V8. The engine converts your JavaScript code into faster machine code. Machine code is low-level code that a computer can run without first interpreting it.

Why Node.js?

Here’s the official definition on the Node.js website:

Node. Js ® is based on
Chrome’s V8 JavaScript engineBuild a JavaScript runtime environment.

Node.js uses an event-driven, non-blocking I/O model, which is lightweight and efficient.

Node.js’ package ecosystem NPM is the largest open source library ecosystem in the world.

We’ve already discussed the first line of this definition: “Node.js® is a JavaScript runtime environment built on Chrome’s V8 JavaScript engine.” Now let’s understand the remaining two lines so that we can figure out why Node.js is so popular.

I/O stands for input/output. It can be anything from reading/writing to a local file to sending HTTP to an API.

I/O takes time, so other functions are blocked.

Consider a situation where we need to get the details for User1 and User2 by requesting a back-end database, and then print them on the screen or console. The response to this request takes time, but two requests for user data can be executed independently and simultaneously.

Blocking I/O (left) vs. non-blocking I/O (right)

Blocking I/O

In the blocking method, data requests for User2 are not started until the data for User1 is printed to the screen.

If this were a Web server, we would have to start a new thread for each new user. But JavaScript is single-threaded (actually not true, but it has a single-threaded event loop, which we’ll discuss later). So this makes JavaScript less suitable for multi-threaded tasks.

This is where non-blocking comes in.

Non-blocking I/O

On the other hand, if you use a non-blocking request, you can initiate a data request for User2 without first waiting for a response to User1’s request. You can start both requests in parallel.

This non-blocking I/O eliminates the need for multithreading because the server can handle multiple requests simultaneously.

JavaScript Event Loop

Here’s a brief step-by-step description of how the JavaScript event loop works.

  1. willmain()Into the call stack.
  2. willconsole.log()Into the call stack. Then immediately run it and pop it up.
  3. willsetTimeout(2000)Into the stack.setTimeout(2000)Is a Node API. When it is invoked, the event callback is registered. The event will wait for 2000 milliseconds before calling back to this function.
  4. After registering with the API,setTimeout(2000)Pop-up from the call stack.
  5. Now the second onesetTimeout(0)Register the same way. We now have two Node APIs waiting to be executed.
  6. After waiting 0 seconds,setTimeout(0)Was moved to the callback queue, the same thing happened insetTimeout(2000).
  7. In the callback queue, the stack of function wait calls is empty because each statement is executed once. This is handled by the event loop.
  8. The last oneconsole.log()Run, andmain()Pop-up from the call stack.
  9. If the event loop detects that the call stack is empty and the callback queue is not empty. It moves the callbacks (in first-in, first-out order) to the call stack and executes.

npm

These are libraries built by an awesome community that can solve most of your regular problems. NPM (Node Package Manager) has a number of packages you can use in your applications to make your development faster and more efficient.

Require

The Require does three things:

  • It loads the modules that Node.js bundles with, such as file systems and HTTP, from the Node.js API.
  • It loads third-party libraries installed from NPM, such as Express and Mongoose.
  • It allows you to require your own files and modularize the project.

Require is a function that takes the parameter “path” and returns module.exports.

The Node module

A Node module is a reusable block of code whose existence does not accidentally affect other code.

You can write your own module and use it in a variety of programs. Node.js has a built-in set of modules that can be used without further installation.

V8 leverages C++ to speed up JavaScript

V8 is an open source runtime engine written in C++.

Javascript => V8 (C ++) => machine code

V8 implements a script named ECMAScript specified in ECMA-262. ECMAScript was created by ECMA International to standardize JavaScript.

V8 can be run on its own or embedded in any C++ program. It has hooks that allow you to write your own C++ code for JavaScript to use.

This actually allows you to add functionality to JavaScript by embedding V8 into your C++ code so that your C++ code does more than the ECMAScript standard.

As Greg Bulmash brought it to my attention, there are many different JavaScript engines besides V8, such as Mozilla’s SpiderMonkey, Microsoft’s Chakra, and so on. More can be found here.

The event

Events are things that we can respond to happen in a program. There are two types of events in Node.

  • System events: From the libuv-based kernel implemented in C++. (for example, the file has been read).
  • Custom Events: JavaScript Core.

Write a Hello World in Node.js

Create the file app.js and add the following to it.

console.log("Hello World!" );

Open the terminal, switch the directory to the folder where the files are saved, and run Node app.js.

It’s as simple as that. The “Hello World” you wrote in Node.js runs.

Finally, you can learn more about Node.js through a number of resources on the Internet.


This article first send WeChat messages public number: front-end pioneer

Welcome to scan the two-dimensional code to pay attention to the public number, every day to push you fresh front-end technology articles


Read on for the other great articles in this column:

  • 12 Amazing CSS Experiment Projects
  • 50 React Interview Questions You Must Know
  • What are the front-end interview questions at the world’s top companies
  • 11 of the best JavaScript dynamic effects libraries
  • CSS Flexbox Visualization Manual
  • React from a designer’s point of view
  • The holidays are boring? Write a little brain game in JavaScript!
  • How does CSS sticky positioning work
  • A step-by-step guide to implementing animations using HTML5 SVG
  • Programmer 30 years old before the monthly salary is less than 30K, which way to go
  • 14 of the best JavaScript data visualization libraries
  • 8 top VS Code extensions for the front end
  • A complete guide to Node.js multithreading
  • Convert HTML to PDF 4 solutions and implementation

  • More articles…