Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
@spazmodius

Description

@spazmodius
Issue body actions
  • 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.Issues 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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.