1

Here is node.js document about response.writeHead(statusCode[, statusMessage][, headers])

I don't understand "statusCode[, statusMessage][, headers]",

Is it stand for one params or more?

  • if one, why writeHead(200, followed a comma?

    if two, why statusCode[, statusMessage][, headers] without a comma?

where means I can pass json?

Is there any document show about these param's rule?

Example:

const body = 'hello world';
response.writeHead(200, {
  'Content-Length': Buffer.byteLength(body),
  'Content-Type': 'text/plain' });
Albert.Qing
  • 4,220
  • 4
  • 37
  • 49
  • 1
    the [] mean those arguments are optional. Also writeHead() is for writing the headers not writing the content. – Patrick Evans Jun 17 '17 at 00:52
  • 2
    Related: [How to read API documentation ...?](https://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs) – Jonathan Lonowski Jun 17 '17 at 00:52
  • @JonathanLonowski. Is it stand for one params or more? – Albert.Qing Jun 17 '17 at 00:55
  • 1
    @Albert.Qing The brackets aren't part of the code you write. They're only relevant within the documentation and are intended to briefly suggest which arguments are required or optional. In this case, an argument for `statusCode` is required, but arguments for either `statusMessage` or `headers` are not. – So, `.writeHead(200)`, `.writeHead(200, 'OK')`, `.writeHead(200, { Age: 12 })`, etc. are valid uses. – Jonathan Lonowski Jun 17 '17 at 00:57
  • Is there any document show about these rules? stil very confused – Albert.Qing Jun 17 '17 at 01:05

1 Answers1

2

The rule is simple - square brackets surrounding any text mean "this text is optional". So

response.writeHead(statusCode[, statusMessage][, headers])

Means all of the following

response.writeHead(statusCode, statusMessage, headers)
response.writeHead(statusCode, statusMessage)
response.writeHead(statusCode, headers)
response.writeHead(statusCode)

If the square brackets were nested as

response.writeHead(statusCode[, statusMessage[, headers]])

it means all of:

response.writeHead(statusCode, statusMessage, headers)
response.writeHead(statusCode, statusMessage)
response.writeHead(statusCode)

Noting that removing the outer set also results in the inner set being removed

Eric
  • 95,302
  • 53
  • 242
  • 374