有人可以在Express plz中解释REQ吗 [英] can someone please explain REQ in express plz

查看:32
本文介绍了有人可以在Express plz中解释REQ吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白我们如何在 express 中使用 req .我不明白服务器可以响应客户端,但是当涉及到 req 对象时,我会感到困惑.当服务器向客户端索要东西时, req 是必需的吗?

I dont get how we use req in express. I undrstand that the server can respond back to client,but when it comes to req object im confused. Is req when the server is asking for somethong from the client?

推荐答案

HTTP是一种应用程序,客户端-服务器协议.每当客户端希望服务器执行操作时,它都必须发出请求.HTTP协议定义了一组可供客户端使用的动作或动词,因此它可以使用一个特定的动词(GET,POST,PATCH,PUT,DELETE等)发出每个请求.客户端使用什么动词都没有关系,只有客户端可以使用其中一个动词来启动与服务器的通信.因此,这就是HTTP GET请求的确切样子:

HTTP is an application, client-server protocol. Everytime that a client want the server to perform an action, it has to make a request. The HTTP protocol defines a set of actions or verbs that are available to the client so it can make each request using one specific verb (GET, POST, PATCH, PUT, DELETE, etc). It doesn't matter what verb the client uses, only the client can initiate a comunication with the server using one of that verbs. So this is how exactly an HTTP GET request looks like:

 GET / HTTP/1.1
 Host: example.com
 User-Agent: curl/7.69.1
 Accept: */*

第一行包含使用的动词(在本例中为 GET ),请求的路径(在本例中为/)和协议版本(在本例中为)HTTP/1.1 .接下来的几行是一组称为该请求的 headers 的键值对,它们可以定义客户端向服务器发出的请求的许多方面.顺便说一句,HTTP服务器永远无法或不会向客户端发起请求,客户端始终是发出请求的客户端,而服务器始终是响应该请求的客户端.例如,该请求的一个方面就是目标主机,该主机出现在标头 host 中,其值为 example.com .在标头的下面,所有HTTP请求都有一个空行,然后是请求的正文,该正文通常包含从客户端发送到服务器的数据.在这种情况下,不会在请求上发送任何数据主体.

The first line contains the verb used, in this case GET, the path requested, in this case /, and the protocol version, in this case HTTP/1.1. The next lines, are a set of key value pairs called the headers of that request, which can define a lot of aspects of the request made by the client to the server. By the way, an HTTP server never could or will start a request to a client, a client always is the one that make the request, and the server is always the one that response that request. One of the aspects of that request for example, is the destination host, that is present in the header host with the value of example.com. Bellow of the headers, all the HTTP requests have a blank line and then the body of the request, which normally contains the data that is sent from the client to the server. In this case, no data body is sent on the request.

Express是基于Node.js上的HTTP模块的HTTP服务器.Express简化了本机Node.js HTTP服务器的工作方式.通常,这是Express应用的外观:

Express is an HTTP server based on the HTTP module available on Node.js. Express simplifies the way that the native Node.js HTTP server works. Here is tipically how an Express app looks like:

const express = require('express');
const app = express();


// This is called a router
app.get('/path', (req, res) => {
    // This router will perform some action with the req object
    // And will send a response to the client 
});

因此,在上面的示例中,Express应用程序上提供的方法 app.get(...)使服务器可以处理来自客户端的GET请求. app.get()方法采用两个参数,即 path 和回调函数. path 参数表示名称服务器之后的字符串,例如,在URL www.example.com/test 中,主机名是 www.example.com ,路径为/test .该方法 app.get()也称为 Router .因此,该示例的路由器将处理对该服务器的 GET 请求,该请求还将/path 值定义为请求发送至的路径.一旦对服务器的请求符合这两个条件,则将触发该回调.

So, on the example above, the method app.get(...), available on Express applications, allows the server to deal with the GET requests that come from the client. The app.get() method takes two arguments, a path and a callback function. The path argument represent the string that goes after the name server, for example, in the URL www.example.com/test, the hostname is www.example.com, and the path is /test. That method app.get() is also called a Router. So, the router of the example will deal with the GET requests to that server which also define /path value as the path the request is sent to. Once a request to the server fit those two conditions, that callback will be triggered.

所以,最后我们得到了答案. res 变量是一个对象(一组用逗号分隔并锁定在花括号中的键对值),该对象包含HTTP请求的数据,并成为一个易于阅读的对象.例如,如果要将控制台使用的路径打印到控制台,则可以像 console.log(req.path)那样打印它,也可以获取该HTTP的所有标头请求,您可以使用 console.log(req.headers). req 对象是Express中5个主要对象之一,实际上,Express文档定义了可用于请求对象( req )的大量方法.要深入了解请求对象,您可以在此链接中查看正式的Express文档..定义到路由器中的回调可以使用 req 对象,以提取客户端请求的信息,进行处理并稍后将响应返回给客户端.

So, finally we get to the answer. The res variable is an object (a set of key-pair values separated by commas and locked into curly braces), that contains the data of the HTTP request, into a friendly legible object. For example, if you want to print to the console the path that the client used, you can print it like this console.log(req.path), or you can get all the headers of that HTTP request, you can use console.log(req.headers). The req object is one of the 5 main objects in Express, in fact the Express documentation defines a ton of methods that you can use with the request object (req). To get deep into the request object, you can see the official Express documentation in this link. The callback defined into the router, can use the req object, to extract information of the client's request, process it and return a response to the client later.

这篇关于有人可以在Express plz中解释REQ吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆