Table of contents
2. Install the Express framework:
3. Express project initialization
4. Execute npm install to install related dependencies
6. Execute the command and start the service
First understand the three main names of NodeJs, event-driven, asynchronous and non-blocking I/O.
Event-driven: Node.js adopts an event-driven model, which means that it executes programs in the order in which events occur. These events can be user operations, network requests, and so on. For example, when you click a button on a web page, an event will be triggered, Node.js will receive the event, process it, and return you a response.
Asynchronous: In JavaScript, when a function is executed, the next function can be executed. However, some functions need to wait for the result of a previous operation before continuing to execute, and then asynchronous methods can be used. Asynchronous functions do not block the execution of the entire program, but return immediately, and then process them after performing subsequent operations.
Non-blocking I/O: Under normal circumstances, reading and writing disks or network I/O are blocking operations, which means that threads will wait until these operations are completed, causing the entire program to slow down. Node.js adopts the non-blocking I/O method. It will poll the system to obtain whether the I/O operation has been completed, so that the thread can perform other operations while waiting for I/O, which improves the concurrency of the program. performance.
Next, let's learn how to use Node.js to build a server.
1. Initialization:
First, we need to install the Node.js environment (please refer to other related articles to install the Node.js environment), and create an empty folder, enter the folder, open the command line, and enter the following code:
npm init
This command will ask you to fill in some basic information about the project, such as project name, author, description, etc. After filling in, a package.json file will be generated, which records the dependency information of the project.
2. Install the Express framework:
Next, we need to install the Express framework, which is a lightweight framework for building web applications, providing functions such as routing and template rendering. Enter the following code on the command line:
npm install express --save
3. Express project initialization
#执行命令
express
The final directory structure is as follows
4. Execute npm install to install related dependencies
5. Other dependencies
npm install serve-favicon : Set the fav icon of the website
npm install morgan:express logging module Morgan
npm install cookie-parser: Middleware for parsing cookies. After adding the middleware, req has the cookies attribute. The value of the cookie can be accessed through req.cookies.xxx.
npm install express-session: session runs on the server side, when the client accesses the server for the first time, the client's login information can be saved.
npm install body-parser: bodyParser is used to parse the content in the body requested by the client, internally uses JSON encoding processing, url encoding processing, and file upload processing.
npm install path: The path module is a system module provided in node.js for formatting or relying on a complete path
npm install jade: Jade is a template engine for Node.js. It borrows a lot from Haml, so its syntax is similar to Haml. Also, Jade also supports spaces.
6. Execute the command and start the service
#执行下面命令
npm start
Visit http://localhost:8080/ , the display is as follows: