Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reply 0 size with no error when using reply.send(stream) or reply.send(buffer) #4412

Closed
2 tasks done
radiorz opened this issue Nov 11, 2022 · 1 comment
Closed
2 tasks done

Comments

@radiorz
Copy link
Contributor

radiorz commented Nov 11, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.0

Plugin version

4.0.0

Node.js version

16.17.1

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

11

Description

reply stream and buffer get 0 size from route.

Steps to Reproduce

I use fastify-cli to build a brand new project.
and add route

fastify.get("/file", async function (request, reply) {
    const filePath = path.join(__dirname, "./aaa.txt");
    console.log(`filePath`, filePath);
    const fileStream = fs.createReadStream(filePath, "utf8");
    reply.header("Content-Type", "application/octet-stream");
    reply.send(fileStream);
  });
  fastify.get("/file1", async function (request, reply) {
    const filePath = path.join(__dirname, "./aaa.txt");
    console.log(`filePath`, filePath);
    fs.readFile(filePath, (err, fileBuffer) => {
      reply.send(err || fileBuffer);
    });
  });

to get it but reply 0 size file

Expected Behavior

reply the right file stream

@climba03003
Copy link
Member

When using async-await, please return the reply object to signal fastify wait for your further response.

fastify.get("/file", async function (request, reply) {
    const filePath = path.join(__dirname, "./aaa.txt");
    console.log(`filePath`, filePath);
    const fileStream = fs.createReadStream(filePath, "utf8");
    reply.header("Content-Type", "application/octet-stream");
    return reply.send(fileStream);
});
fastify.get("/file1", async function (request, reply) {
  const filePath = path.join(__dirname, "./aaa.txt");
  console.log(`filePath`, filePath);
  fs.readFile(filePath, (err, fileBuffer) => {
    reply.send(err || fileBuffer);
  });
  return reply
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants