mcavage/node-restify — Issue #53: sendError: only code on HEAD request
Author: fnogatz
Created: 2011-12-23 21:41:14+00:00
State: open

Hi!
Following use case: If I use the same prehandler for multiple request types, e.g. for checking if a requested resource exists, I want to send an error with res.sendError(...). While this is correct for GET routes, it will create an "[HttpError: Content-Length 62 didn't match: 0]" error with HEAD.

Example:

var restify = require('restify');

var server = restify.createServer();

function handler(req, res, next) {
res.send(200);
}
function pre(req, res, next) {
res.sendError(restify.newError({
httpCode: 404
}));
}

server.head('/test', [pre], handler);
server.get('/test', [pre], handler);

server.listen(3000);

var client = restify.createClient({
url: 'http://localhost:3000',
retryOptions: { retries: 0 }
});
client.head('/test', function(err, headers, res) {
if (err) {
console.log(err);
}
});

will return:

{ [HttpError: Content-Length 62 didn't match: 0]
_isError: true,
name: 'HttpError',
httpCode: 500,
restCode: { httpCode: [Getter], message: [Getter] },
message: 'Content-Length 62 didn\'t match: 0',
headers:
{ connection: 'close',
date: 'Fri, 23 Dec 2011 21:33:48 GMT',
server: 'node.js',
'x-request-id': '6738b428-d152-4bbb-a44b-0a4016d97a5d',
'x-response-time': '2',
'content-length': '62',
'content-md5': 'U8+IRkYtzh/Ck7k6mGUxrA==',
'content-type': 'application/json',
'access-control-allow-origin': '*',
'access-control-allow-methods': 'OPTIONS, HEAD, GET',
'access-control-allow-headers': 'Accept, Content-Type, Content-Length, Date, X-Api-Version',
'access-control-expose-headers': 'X-Api-Version, X-Request-Id, X-Response-Time' } }

When the clients requests per GET everything is fine.
I know that I could check which type of request it is in the handler and use a res.send(404) for HEADs. But it would be nice and more simple if it would be checked automatically.

Merry Christmas,
Falco