I'm adding a trailer to a response in Node and I just can't seem to get the trailer to be received as part of the message. I want to use it to send timing information back to a load balancer I've written in Node, so it can monitor if requests are queuing for a long time in a particular worker.
I'm trying with just a basic app to get it working first, which I've taken from the documentation for addTrailers:
var http = require("http");
var server = http.createServer(function (req, res) {
var fileData = "Hello World!";
res.writeHead(200, { 'Content-Type': 'text/plain',
'Trailer': 'my-trailer' });
res.write(fileData);
res.write("\r\n");
res.addTrailers({'my-trailer': 'test'});
res.end();
});
server.listen(12345);
I'm using node-http-proxy in the load balancer and it doesn't receive the trailers, Charles Proxy doesn't show them and neither does curl 7.35. However, wireshark does show the data at the end of the packet:
c
Hello World!
2
0
my-trailer: test
Does anyone know what you have to do to get Node to parse a trailer from a message rather than discard it?
Having investigated quite deeply, trailers aren't set until http.ClientRequest
emits the end
event (which makes sense, I guess). This isn't documented on node-http-proxy but looking at the code it is emitted.
So we can set up a handler for the end
event
proxy = httpProxy.createProxyServer();
proxy.on("end", function(req, res, proxyRes){
console.log(proxyRes.trailers);
});
proxy.listen(8000);
and my trailer is there!
{ 'x-worker-duration': '7.397' }