0

I was reading the documentation for the HTTP module in nodejs, and I'm a bit confused with the syntax used to display parameters. For example, here's the syntax for the createServer method.

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

Could someone break down what the arguments mean? Specifically, what does the comma before "request listener" indicate?

Another one that's even more confusing to me:

http.request(url[, options][, callback])

Thanks!

bugsyb
  • 5,662
  • 7
  • 31
  • 47
  • Square brackets mean a parameter is optional. – Barmar Jul 28 '21 at 17:00
  • The comma is the normal separator between arguments. – Barmar Jul 28 '21 at 17:01
  • 1
    Had a full answer for the guy that could be helpful.... sadly we are very quick here with closing questions :/ – U Rogel Jul 28 '21 at 17:08
  • 1
    `http.request(url[, options][, callback])` http.request gets three params: 1. url - required 2. options - optional 3. callback - optional `http.createServer([options][, requestListener])` http.createServer gets two params: 1. options - optional 2. requestListener - optional In both functions make sure to keep the order of the params. So `url` is first, then `options`, then `callback`. Make sure to read what each parameter expects. In this case `url` expects a string, `options` an object with different options, `callback` a function to run once the server is successfully created. – U Rogel Jul 28 '21 at 17:10
  • Actually one additional question @URogel: I called the `createServer()` method just using the callback and it seemed to work. I just put the callback straight into the function argument without making an effort to skip the first argument ([options]). How does this work? I assumed I would have had to pass in "undefined" to skip that argument. Am I missing something? – bugsyb Jul 28 '21 at 17:19
  • 1
    @bugsyb if you give second argument a function then createServer knows it is not options but the callback function. But don't count on this behavior for every builtin function. – U Rogel Jul 28 '21 at 17:25

0 Answers0