node.js basic sending request and response

The client accesses the server, and the server responds after processing the data sent by the client

1. Client access server

Access through the http protocol

What is the http protocol? Hypertext Transfer Protocol

For a request, there is a request body and a request body:

Request header: key-value pair to save client information
Request body: save information sent by post

1. The server will process the request sent by the client and encapsulate the data sent by the browser to the server into a Request object

2. Then execute the callback function createServe to assign the object encapsulated in the previous step to the first parameter req

3. In the callback function, the data sent by the browser to the server in this request can be obtained through req

Code: 

// 1.导入http模块
// 它是一个内置模块,返回的是一个对象
const http = require('http')

// 2.创建一个服务
// 这个服务就是用来接收来自客户端的http请求 并且可以处理这个请求
// 这个方法需要传入1个回调函数 两个形参:req res
// 这个回调函数 在服务接收到来自客户端的http请求的时候 就会自动执行
const serve = http.createServer((req, res) => {
    console.log(`接收到了来自客户端${getClientIp(req)}请求`);
    res.end('Hello node.js')
});

// 3.开启这个服务
// 监听:
//     参数1: 服务工作的端口号
//     参数2:回调函数 在服务器开启以后自动执行
serve.listen(8000, () => {
    console.log('服务成功开启');
})


function getClientIp(req) {
    return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
};

The port is similar to the specific window for going to the hospital to see a doctor in life, such as the payment window, each window plays a different role; IP is the address of the hospital

There are several methods to focus on:
 

  a. req.headers Get the request body message sent by the client to the server in this request

  b. req.url Get the URL part of the client request of this request starting with '/' if there is no URL value is '/'

  c. req.method Obtain the HTTP method get/post of this request client request server


2. The server responds after processing the data sent by the client

 

// 1.导入http模块
// 它是一个内置模块,返回的是一个对象
const http = require('http')
const serve = http.createServer((req, res) => {
    console.log(`接收到了来自客户端${getClientIp(req)}请求`);
    res.setHeader('Content-Type', 'text/html;charset=utf8')
    // write方法向浏览器写入数据 直接收字符串和Buffer数据
    res.write('<h1>王小美</h1>');
    // 设置响应状态码
    res.statusCode = 200;
    // end方法代表本次请求结束了,一定要写的
    // 可以传递参数:表示结束并将数据追加到响应体中
    res.end('Hello node.js');
});

serve.listen(8000, () => {
    console.log('服务成功开启');
})

function getClientIp(req) {
    return req.headers['x-forwarded-for'] ||
        req.connection.remoteAddress ||
        req.socket.remoteAddress ||
        req.connection.socket.remoteAddress;
};

 Key points:
       Response header:
        Content-Type: Tell the browser what type of data the server sends to you.
                      You can also tell the browser the encoding format of
         text/html. The data sent to the browser by the server is html, and the browser will parse it according to html.
         text/plain The data sent by the server to the browser is plain text and the browser will display it as it is

res is the abbreviation of response, which means response

req is the abbreviation of request, which means request

Guess you like

Origin blog.csdn.net/Motion_zq/article/details/125098796