与 (req, res) 相似的是

huangapple go评论83阅读模式
英文:

is <Function> similar as (req,res)

问题

  1. (req, res) vs. <Function> 的区别是什么?

  2. (req, res) 在哪里有解释?我想知道如何配置或访问 reqres 中的属性。

英文:

i am new to nodejs. referring to this link section http.createServer([options][, requestListener])
i know how to create the options object. but for the [, requestListener], it is stated in the docs that it is a &lt;Function&gt;. when i clicked on the link of &lt;Function&gt; which is here
it indicates that that &lt;Function&gt; is a normal function that could be lambda, however, in the docs of http.createServer([options][, requestListener]) as stated in the first link, it demonstrate the following:

http.createServer((req, res)

as far as i understood, the (req, res) is two different callbacks not a normal &lt;Function&gt;
to some extent. i am slightly befuddled.

1.would you please show me the difference between (req,res) and &lt;Function&gt;

2.where in docs (req,res) are explained. i want to know how can i configure or access properties of either of req and res

答案1

得分: 2

你误读了代码。(req, res) 不是传递给 createServer 的参数。整个内容是一个箭头函数表达式,用于定义匿名函数:

(req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    data: 'Hello World!',
  }));
}

它在很大程度上(但不完全)等同于:

function serverHandler(req, res) {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    data: 'Hello World!',
  }));
}

不同之处(在这里并不是非常重要,但为了完整起见)是:后者可以是函数表达式或函数语句,而前者始终是表达式;前者不重新定义 this,而后者会;前者没有分配给变量,而后者可以(在这种情况下是 serverHandler)。

根据后者对 serverHandler 的定义,你可以这样使用它:

http.createServer(serverHandler)

匿名箭头函数表达式在这个后者的例子中只是做与函数标识符相同的工作。


编辑 关于如何查找有关 reqres 的更多文档的信息:

根据 createServer 的文档:

requestListener 是自动添加到 request 事件的函数。

根据 'request' 事件 的文档,事件回调接收两个参数:request,一个 IncomingMessage 的实例,以及 response,一个 ServerResponse 的实例。

英文:

You are misreading the code. (req, res) is not a parameter to createServer. This entire thing is an arrow function expression, a way to define an anonymous function:

(req, res) =&gt; {
  res.writeHead(200, { &#39;Content-Type&#39;: &#39;application/json&#39; });
  res.end(JSON.stringify({
    data: &#39;Hello World!&#39;,
  }));
}

It is largely (but not completely) equivalent to:

function serverHandler(req, res) {
  res.writeHead(200, { &#39;Content-Type&#39;: &#39;application/json&#39; });
  res.end(JSON.stringify({
    data: &#39;Hello World!&#39;,
  }));
}

The differences (not really important here, but for completeness' sake) are: the latter can be a function expression or a function statement, while the former is always an expression; the former does not redefine this while the latter does; and the former is not assigned to a variable, while the latter may be (serverHandler in this case).

Given the latter definition of serverHandler, you could use it as follows:

http.createServer(serverHandler)

An anonymous arrow function expression is just doing the same job as a function identifier in this latter example.


EDIT on where to find more documentation on req and res:

Per documentation of createServer:

> The requestListener is a function which is automatically added to the &#39;request&#39; event.

Per documentation of &#39;request&#39; event, the event callback gets two parameters: request, an instance of IncomingMessage, and response, an instance of ServerResponse.

答案2

得分: 2

reqres 参数传递给回调函数与相应的 request 事件的 requestresponse 属性相同。

> 事件:'request'
> 添加于:v0.1.0
> - request &lt;http.IncomingMessage&gt;
> - response &lt;http.ServerResponse&gt;

然后,您可以按照这些数据类型的文档链接查看如何在您的服务器中使用它们。

英文:

The req and res arguments to the callback are the same as the request and response properties of the corresponding request event.

> Event: 'request'
>Added in: v0.1.0
>- request &lt;http.IncomingMessage&gt;
>- response &lt;http.ServerResponse&gt;

You can then follow the links to the documentation of those datatypes to see how you use them in your server.

huangapple
  • 本文由 发表于 2023年6月22日 13:27:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定