Module 01 · Introduction to Server-Side JavaScript服务器端 JavaScript 入门
Getting started with Node.jsNode.js 入门
A full-stack application includes the following components:
一个全栈应用程序包括以下组件:
The Client Side: 客户端:
The website and the mobile application that are user-facing.
网站和面向用户的移动应用程序。
The Server Side: 服务器端:
Processes any requests from the client side and sends responses back to the client. In today's world, the cloud hosts the web server, application server, and database.
处理客户端的任何请求并将响应发送回客户端。在当今世界,云托管了 Web 服务器、应用服务器和数据库。

Open Source and Cross-Platform:
开源和跨平台:
JavaScript is an ideal choice for client-side validation of HTML pages due to its versatility and cross-platform compatibility. Recognizing its ease of use, JavaScript was further adapted for server-side coding, resulting in Node.js. Node.js functions as a runtime environment, and what's more, it requires no special licenses for use. Additionally, a plethora of packages and libraries have been developed for use with Node.js, thanks to its open-source nature. Furthermore, Node.js code can seamlessly run across various operating systems such as Linux, Windows, and Mac OSX.
JavaScript 是用于 HTML 页面客户端验证的理想选择,因为它具有多功能性和跨平台兼容性。鉴于其易用性,JavaScript 还被进一步用于服务器端编码,从而产生了 Node.js。Node.js 作为一种运行环境,更重要的是,它使用时无需特殊许可证。此外,由于 Node.js 的开源性质,已经开发了大量的包和库用于与 Node.js 一起使用。此外,Node.js 代码可以在各种操作系统上无缝运行,例如 Linux、Windows 和 Mac OSX。
V8 Engine: V8 引擎:
Every piece of code you write needs to undergo processing and conversion into a machine-readable form. In the realm of Node.js, JavaScript code is executed using the Google V8 engine. Renowned for its high performance, V8 is an open-source engine developed by Google, and it is integrated into all Google Chrome browsers. Moreover, modern browsers like Microsoft Edge and Firefox also employ the V8 engine for executing Node.js code.
每一段你编写的代码都需要经过处理和转换成机器可读的形式。在 Node.js 领域,JavaScript 代码是通过 Google V8 引擎执行的。以其高性能而闻名,V8 是由 Google 开发的开源引擎,并集成到所有 Google Chrome 浏览器中。此外,现代浏览器如 Microsoft Edge 和 Firefox 也使用 V8 引擎来执行 Node.js 代码。
Event-Driven, Asynchronous, Non-Blocking, Single-Threaded:
事件驱动、异步、非阻塞、单线程:
Server processes can be categorized as either "single-threaded" or "multi-threaded". In a single-threaded environment, only one command is processed at any given time, while in a multi-threaded environment, multiple commands can be processed simultaneously. Despite being single-threaded, Node.js excels in performance due to its asynchronous and non-blocking nature. This means that while a process is being executed, the program does not need to wait until it finishes. Node.js is event-driven, meaning that when it performs an input/output (I/O) operation, such as reading from the network or accessing a database or file system, an event is triggered. Instead of blocking the thread and consuming processor time while waiting, Node.js resumes operations when the response is received, or when the corresponding event occurs. This non-blocking behavior enables the server to remain responsive and handle multiple tasks concurrently, akin to a multi-threaded environment.
服务器进程可以分为“单线程”或“多线程”两种。在单线程环境中,任何给定时间只处理一个命令,而在多线程环境中,可以同时处理多个命令。尽管是单线程,Node.js 由于其异步和非阻塞的特性,在性能上表现出色。这意味着在进程执行时,程序不需要等待其完成。Node.js 是事件驱动的,这意味着当它执行输入/输出(I/O)操作,如从网络读取或访问数据库或文件系统时,会触发一个事件。Node.js 不会在等待响应或相应事件发生时阻塞线程和消耗处理器时间,而是在收到响应或事件发生时恢复操作。这种非阻塞行为使服务器保持响应性,并能够同时处理多个任务,类似于多线程环境。
JSON Payload JSON 负载
JSON stands for JavaScript Object Notation, formatted as "key-value pairs". The payload represents the data transmitted between the client and the server. When the client needs to send data to the server, it does so in the form of a JSON object, as illustrated in the example below:
JSON 代表 JavaScript 对象表示法,格式为“键值对”。有效载荷表示客户端和服务器之间传输的数据。当客户端需要向服务器发送数据时,它会以 JSON 对象的形式发送,如下例所示:
{
"name": "John",
"age": "24",
"email": "johnparker@gmail.com"
}
The above object is a JSON. When this data is sent as part of the request, its values can be extracted simply by using request.name and so on.
上述对象是一个 JSON。当这部分数据作为请求发送时,其值可以通过使用 request.name 等简单提取。
Similarly, the response is also sent as JSON. An example is provided below:
同样,响应也以 JSON 格式发送。以下是一个示例:
{
"status": "ok",
"message": "Successfully added"
}
Express Framework Express 框架
While Node.js provides packages to create a server, the Express framework simplifies the process of creating APIs and endpoints. An API endpoint is a specific point of entry for a request from the client to the server.
虽然 Node.js 提供了创建服务器的包,但 Express 框架简化了创建 API 和端点的过程。API 端点是客户端向服务器发送请求的特定入口点。
Next steps 下一步
In this course, you will learn how to perform server-side coding with Node.js using the Express framework.
在这个课程中,您将学习如何使用 Express 框架以 Node.js 进行服务器端编码。