Node.js Tutorial
Simply put, Node.js is JavaScript that runs on the server.
Node.js is a platform built on the Chrome JavaScript runtime.
Node.js is an event-driven, I/O-based server-side JavaScript environment built on Google's V8 engine; the V8 engine executes JavaScript very quickly and performs extremely well.
Who should read this tutorial?
If you are a front-end programmer who does not know dynamic programming languages such as PHP, Python, or Ruby, and you want to create your own service, then Node.js is an excellent choice.
Node.js is JavaScript that runs on the server; if you are familiar with JavaScript, you will find it very easy to learn Node.js.
Of course, if you are a backend 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 studying 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, or Java, it will help you understand Node.js programming more quickly.
Node.js Features
- Single-threaded and event-driven architecture: Node.js uses a single thread to handle requests, but through its event-driven and non-blocking I/O operations, it can efficiently handle a large number of concurrent connections without blocking the thread.
- Asynchronous and non-blocking I/O: This enables Node.js to handle high-concurrency 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.
- Vast ecosystem: Node.js has an ecosystem called npm (Node Package Manager), which contains a wealth of third-party libraries and modules that can be used to extend functionality and accelerate development.
Application Scenarios of Node.js
- 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: Such as chat applications, real-time collaboration tools, and more.
- API services: Node.js is well suited for developing RESTful APIs and GraphQL services.
- command line tool: Node.js can also be used to build CLI tools, leveraging its easy-to-use libraries and package manager for rapid development.
Version in use
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.
The 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 execute it with the node command:
node helloworld.js
After the program runs, if everything is normal, Hello World will be output in the terminal.
Interactive mode
Open the terminal, type node to enter the interactive command mode, where you can enter a code statement and have it executed and display the result immediately, for example:
$ node
> console.log('Hello World!');
Hello World!
Gif Example Demonstration
Next, we demonstrate the example operations with a Gif image: