Node.js installation tutorial and its introduction

Introduction to Node.js

Essence: JS on the server side

  1. Introduce the required module: we can use the require command to load the Node.js module.

  2. Create a server: The server can listen to client requests, similar to HTTP servers such as Apache and Nginx.

  3. It is easy to create a server that receives requests and responds to requests. The client can use a browser or terminal to send HTTP requests, and the server returns response data after receiving the request.

Node.js installation tutorial

The Node.js installation package and source code download address is: https://nodejs.org/en/download/ .

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Check whether Node.js is configured in the PATH environment variable, click Start=》Run=》input "cmd" => input the command "path", and output the following result:

PATH=C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
c:\python32\python;C:\MinGW\bin;C:\Program Files\GTK2-Runtime\lib;
C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\nodejs\;
C:\Users\rg\AppData\Roaming\npm

Insert picture description here

Case study

Step 1: Introduce the required module

We use the require command to load the http module and assign the instantiated HTTP to the variable http. The example is as follows:

var http = require("http");

Step two, create a server

Next, we use the http.createServer() method to create a server, and use the listen method to bind port 8888. The function receives and responds to data through request and response parameters.

The example is as follows, create a file called server.js in the root directory of your project, and write the following code:

var http = require('http');

http.createServer(function (request, response) {
    
    

    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {
    
    'Content-Type': 'text/plain'});

    // 发送响应数据 "Hello World"
    response.end('Hello World\n');
}).listen(8888);


// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

With the above code, we have completed a working HTTP server.

Use the node command to execute the above code:

node server.js
Server running at http://127.0.0.1:8888/

Insert picture description here

Analyze the HTTP server of Node.js

The first line requires the http module that comes with Node.js and assigns it to the http variable.
Next we call the function provided by the http module: createServer. This function will return an object. This object has a method called listen. This method has a numeric parameter that specifies the port number the HTTP server listens on.

Guess you like

Origin blog.csdn.net/david2000999/article/details/114970424