英文:
is <Function> similar as (req,res)
问题
-
(req, res)vs.<Function>的区别是什么? -
(req, res)在哪里有解释?我想知道如何配置或访问req和res中的属性。
英文:
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 <Function>. when i clicked on the link of <Function> which is here
it indicates that that <Function> 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 <Function>
to some extent. i am slightly befuddled.
1.would you please show me the difference between (req,res) and <Function>
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)
匿名箭头函数表达式在这个后者的例子中只是做与函数标识符相同的工作。
编辑 关于如何查找有关 req 和 res 的更多文档的信息:
根据 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) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!',
}));
}
It is largely (but not completely) equivalent to:
function serverHandler(req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!',
}));
}
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 'request' event.
Per documentation of 'request' event, the event callback gets two parameters: request, an instance of IncomingMessage, and response, an instance of ServerResponse.
答案2
得分: 2
req 和 res 参数传递给回调函数与相应的 request 事件的 request 和 response 属性相同。
> 事件:'request'
> 添加于:v0.1.0
> - request <http.IncomingMessage>
> - response <http.ServerResponse>
然后,您可以按照这些数据类型的文档链接查看如何在您的服务器中使用它们。
英文:
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 <http.IncomingMessage>
>- response <http.ServerResponse>
You can then follow the links to the documentation of those datatypes to see how you use them in your server.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论