writeHead doesn't work as expected when passing headers as second parameter
See original GitHub issueHey,
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:
- Created 6 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@yvele you were right, it depends on node version (it works against 6.x)
@gunzip what version of Node.js are you using? Because it’s only compatible with
6.x.x