node js documentation

addictzz

Senior Member
Joined
May 30, 2010
Messages
2,495
Reaction score
0
I have question on how to read node js documentation. I often see these kinds of argument notation below:

- http.createServer([options][, requestListener])
*arguments in square bracket ie. [options]
*square bracket, comma, and argument name ie. [, requestListener]
- emitter.emit(eventName[, ...args])
* [,...args]?
- http.get(options[, callback])
* arguments with no bracket ie. options

what do these argument notation mean?
 
Last edited:

addictzz

Senior Member
Joined
May 30, 2010
Messages
2,495
Reaction score
0
I can always guess by reading thru the explanation. But I would like to know officially what they meant.

PS: After more searches around the net, found out that this square bracket around arguments mean it is optional argument. And a comma inside square bracket means that it is optional argument with a default value.
 

jgyy1990

Arch-Supremacy Member
Joined
Apr 28, 2012
Messages
12,431
Reaction score
88
http.createServer is meant to create the server in either production mode based on your hosting platform or development mode whichever your localhost port is.

http.get, http.put, http.post, http.delete and etc are just basic CRUD. you need the callback to do basic request, response or to the next "function" or "object".

I am not focused on backend logic, so correct me if i am wrong.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
I have question on how to read node js documentation. I often see these kinds of argument notation below:

- http.createServer([options][, requestListener])
*arguments in square bracket ie. [options]
*square bracket, comma, and argument name ie. [, requestListener]
- emitter.emit(eventName[, ...args])
* [,...args]?
- http.get(options[, callback])
* arguments with no bracket ie. options

what do these argument notation mean?

I might not entirely correct, but across numerous documentations that I have come across. The above is a more a loosely created notation to explain the arguments formats.

[ ... ] is normally associated with OPTIONAL

The comma placed inside the square brackets is similar to the regex notation as such

Code:
A(,B)?

meaning the regex is match

Code:
A
A,B

meaning if you are going to put in a 2nd argument, it should be separated from the first using a comma.

Hence

Code:
http.createServer([options][, requestListener])

Would means you can invoke the createServer method using the following possible arguments

Code:
http.createServer();
http.createServer({ .... });
http.createServer({ .... }, function() { ... });

That being said, the documentation is loosely presented for example.

The above method syntax does not in anyway denote that the first OBJECT "options" can be optionally left out and immediately just specify the 2nd argument, in which the syntax should allow the comma(,) be placed immediately in front of the function argument as such

Code:
http.createServer(, function() { ... });

Obviously, if you try doing this, the javascript parser will choke. So this is more of common sense instead of a formal syntax declaration.

Hence you can write either of these for the above case

Code:
http.createServer(function() { ... })
http.createServer(() => { ... })

This is why I say this is a very loosely written documentation :)

A more formal way of writing the above intention should be the following

Code:
http.createServer([options | requestListener])
http.createServer(options, requestListener)

Code:
emitter.emit(eventName[, ...args])

The above, it should be interpreted as eventName is mandatory, and there could be zero or more arguments the follow, as such.

Code:
emitter.emit(eventName)
emitter.emit(eventName, arg1)
emitter.emit(eventName, arg1, arg2, ..., argN)

The last I will leave to you interpret it for yourself :)
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Community Guidelines and Standards, Terms of Service and Member T&Cs for more information.
Top