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
2
Answers
You are misreading the code.
(req, res)
is not a parameter tocreateServer
. This entire thing is an arrow function expression, a way to define an anonymous function:It is largely (but not completely) equivalent to:
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: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
andres
:Per documentation of
createServer
:Per documentation of
'request'
event, the event callback gets two parameters:request
, an instance ofIncomingMessage
, andresponse
, an instance ofServerResponse
.The
req
andres
arguments to the callback are the same as therequest
andresponse
properties of the correspondingrequest
event.You can then follow the links to the documentation of those datatypes to see how you use them in your server.