When I try to send a HEAD
request for sendFile
I get the following error:
app.head(filePath, { logLevel: LOG_LEVEL }, async (request, reply) => {
console.log('head');
try {
const { '*': uriPath } = request.params;
const isFile = !!uriPath.match(/\.[a-zA-Z0-9]{1,5}(\?.*)?/);
if (isFile) {
setCacheControl(reply, FILE_CACHE_CONTROL_MAX_AGE);
reply.sendFile(uriPath);
} else {
const indexPath = 'index.html';
const indexStr = await fs.readFile(path.join(serveRoot, indexPath), {
encoding: 'utf-8',
});
const indexPayload = await injectEnv(indexStr);
setCacheControl(reply, INDEX_CACHE_CONTROL_MAX_AGE);
reply.type('text/html');
reply.send(indexPayload);
}
} catch (e) {
console.error(e);
}
});
web_1 | {"level":50,"time":1580244056047,"pid":1,"hostname":"3ee631923a16","reqId":5,"err":{"type":"FastifyError","message":"FST_ERR_PROMISE_NOT_FULLFILLED: Promise may not be fulfilled with 'undefined' when statusCode is not 204","stack":"FastifyError [FST_ERR_PROMISE_NOT_FULLFILLED]: FST_ERR_PROMISE_NOT_FULLFILLED: Promise may not be fulfilled with 'undefined' when statusCode is not 204\n at /usr/src/server/node_modules/fastify/lib/wrapThenable.js:34:30\n at processTicksAndRejections (internal/process/task_queues.js:85:5)","name":"FastifyError [FST_ERR_PROMISE_NOT_FULLFILLED]","code":"FST_ERR_PROMISE_NOT_FULLFILLED","statusCode":500},"msg":"Promise may not be fulfilled with 'undefined' when statusCode is not 204","v":1}
The way that express handles this is by simply passing through HEAD
requests to the GET
method, and then letting send
(the underlying package that sends responses for both fastify and express) handle it here by not sending the output but sending the headers.
But fastify seems to incorrectly mark this as an error here