Node.jsTutorial

nodejs

Simply put, Node.js is JavaScript running on the server side.

Node.js is a platform built on the Chrome JavaScript runtime.

Node.js is an event-driven I/O server-side JavaScript environment, based on Google's V8 engine. The V8 engine executes JavaScript very quickly and with excellent performance.


Who is this tutorial for?

If you are a front-end programmer, you don't know dynamic programming languages like PHP, Python, or Ruby, and you want to create your own services, then Node.js is a very good choice.

Node.js is JavaScript running on the server side. If you are familiar with JavaScript, you will learn Node.js very easily.

Of course, if you are a back-end programmer and want to deploy some high-performance services, then learning Node.js is also a very good choice.


What you need to know before learning this tutorial

Before continuing with this tutorial, you should understand some basic computer programming terms. If you have studied programming languages such as JavaScript, PHP, Java, etc., it will help you understand Node.js programming faster.


Node.js Features

  • Single-threaded and event-driven architecture: Node.js uses a single thread to handle requests, but through the characteristics of event-driven and non-blocking I/O operations, it can efficiently handle a large number of concurrent connections without blocking threads.
  • Asynchronous and non-blocking I/O: This allows Node.js to handle highly concurrent requests, making it very suitable for I/O-intensive applications such as file reading, database operations, and network requests.
  • Cross-platform support: Node.js can run on multiple operating systems such as Windows, Linux, and macOS.
  • Huge ecosystem: Node.js has an ecosystem called npm (Node Package Manager), which contains a rich collection of third-party libraries and modules that can be used to extend functionality and speed up development.

Node.js Application Scenarios

  • Web server: Due to its efficient ability to handle concurrent requests, Node.js is often used to build fast, scalable web servers.
  • Real-time applications: For example, chat applications, real-time collaboration tools, etc.
  • API services: Node.js is very suitable for developing RESTful API and GraphQL services.
  • Command-line tools: Node.js can also be used to build CLI tools, leveraging its easy-to-use libraries and package manager for rapid development.

Version Used

We can use the following command to check the current Node version:

$ node -v
v4.4.3

Note:There may be differences between different versions.


First Node.js Program: Hello World!

Script Mode

The following is our first Node.js program:

Example

console.log("Hello World");

Run Example »

Save the file with the name helloworld.js, and run it with the node command:

node helloworld.js

After the program is executed, if everything is normal, it will output Hello World in the terminal.

Interactive Mode

Open the terminal, type node to enter the command interactive mode. You can enter a code statement, which will be executed immediately with the result displayed. For example:

$ node
> console.log('Hello World!');
Hello World!

Gif Example Demonstration

Next, we will demonstrate the example operation through a Gif:

Other extensions