Skip to content

response.writeHead() does not default statusMessage if it's undefined #32395

Closed
@spazmodius

Description

@spazmodius
  • Version: 12.16.1
  • Platform: Windows
  • Subsystem: http

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

works as expected if statusMessage is omitted, supplying a default value.
However, if undefined is passed as a placeholder then it unexpectedly ignores the headers argument.

Why would I want this? Imagine a function like:

function send({ statusCode, statusMessage, headers, body } = { statusCode: 404 }, res) {
  return new Promise((resolve, reject) => {
    res.writeHead(statusCode, statusMessage, headers)
      .end(body)
      .on('finish', resolve)
      .on('error', reject)
  })
}

You can see I depend on a single signature to work whether statusMessage is supplied or not. Otherwise, I have to:

  if (statusMessage)
    res.writeHead(statusCode, statusMessage, headers)
  else
    res.writeHead(statusCode, headers)

Not the end of the world, but it lacks elegance.

Maybe this is not a bug, but wrong expectations?
Perhaps. But this expectation aligns with how javascript default parameters behave.

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions