question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

writeHead doesn't work as expected when passing headers as second parameter

See original GitHub issue

Hey,

first of all: thanks for providing this library!

I ran into an issue with another library where writeHead is called like this:

res.writeHead(302, {
   'Access-Control-Allow-Origin': '*',
   'Location': path
});

It took me some time to realize, that azure-function-express provides its own implementation of this method. It seems that there is a incompatibility, as the original one allows for the second statusMessage to be omitted as you can see in the Node documentation:

const body = 'hello world';
response.writeHead(200, {
  'Content-Length': Buffer.byteLength(body),
  'Content-Type': 'text/plain' });

https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

The implementation provided by azure-function-express doesn’t support this call signature:

function writeHead(context, statusCode, statusMessage, headers) {
  // 1. Status code
  statusCode |= 0; // eslint-disable-line no-param-reassign
  if (statusCode < 100 || statusCode > 999) {
    throw new RangeError(`Invalid status code: ${statusCode}`);
  }

  // 2. Status message
 this.statusMessage = statusMessage || statusCodes[statusCode] || "unknown";

  // 3. Headers
  if (this._headers) {
    // Slow-case: when progressive API and header fields are passed.
    if (headers) {
      const keys = Object.keys(headers);
      for (let i = 0; i < keys.length; i++) {
        const k = keys[i];
        if (k) {
          this.setHeader(k, headers[k]);
        }
      }
    }
    // only progressive api is used
    headers = this._renderHeaders(); // eslint-disable-line no-param-reassign
  }

  // 4. Sets everything
  context.res.status = statusCode;
  context.res.headers = headers;
}

https://github.com/yvele/azure-function-express/blob/master/src/OutgoingMessage.js#L44

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
gunzipcommented, Oct 20, 2017

@yvele you were right, it depends on node version (it works against 6.x)

0reactions
yvelecommented, Oct 18, 2017

@gunzip what version of Node.js are you using? Because it’s only compatible with 6.x.x

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js response.writeHead() Method - GeeksforGeeks
The last argument, headers, are the response headers. Optionally one can give a human-readable statusMessage as the second argument.
Read more >
Error: Can't set headers after they are sent to the client
When a POST request is sent to /api/route1 it will run every line in the callback. A Can't set headers after they are...
Read more >
HTTP | Node.js v19.3.0 Documentation
It parses a message into headers and body but it does not parse the actual headers or the body. See message.headers for details...
Read more >
js.node.http.ServerResponse - HXNODEJS - API documentation
It is passed as the second parameter to the 'request' event. ... caching internally, and the getHeader() on the header will not yield...
Read more >
Request Handling :: CIS 526 Textbook
In this way, you can write the entire sending of a response on a single line. The parameters to response.writeHead() are the status...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found