当我尝试发送HEAD
请求时,sendFile
我收到以下错误:
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}
express 处理这个问题的方式是简单地将HEAD
请求传递给GET
方法,然后让send
(为 fastify 和 express 发送响应的底层包)在这里通过不发送输出而是发送 headers 来处理它。
但是 fastify 似乎在这里错误地将其标记为错误