0

i wish to set the same header as the following one but with fastify framework

// working example with express    
const head = {
          'Content-Range': `bytes ${start}-${end}/${fileSize}`,
          'Accept-Ranges': 'bytes',
          'Content-Length': chunksize,
          'Content-Type': 'video/mp4',
        }

Right now i am doing that (with fastify), but it does not work(or look like)

reply
    .header('Content-Type','video/mp4')
    .header('Content-Lenght', chunksize)
    .header('Accept-Range', 'bytes')
    .header('Content-Range', `bytes ${start}-${end}/${size}`)
    .send(str)

I did not find any example of multiple header with fastify. The header's contents does not matter, i just need to know how to set it up correctly. Thank you

Jerome
  • 294
  • 2
  • 14
  • Your code is fine, what is not working? did you added a `foo: bar` response header? – Manuel Spigolon Jun 11 '21 at 11:58
  • @Manuel, Thank you for you feedback. With the first header (with express, or node-core http-module so) the range works when i display the video in the browser after Streaming it. But when i run the exact same code with fastify, only modifying the header, the range does not work any-more. So i had a doubt about my header... But as you said that is fine, i probably need to look for the issue somewhere else. – Jerome Jun 11 '21 at 13:16
  • How you are managing the stream to send ranges? – Manuel Spigolon Jun 11 '21 at 13:31
  • The issue is from the headers, as you said Manuel the header is fine but the status-code is missing, i added a line `.code(206)` and it worked as expected. – Jerome Jun 12 '21 at 08:19

1 Answers1

0

You can set multiple headers at once using the headers method:

reply
  .code(206)
  .headers({
    'Content-Range': `bytes ${start}-${end}/${fileSize}`,
    'Accept-Ranges': 'bytes',
    'Content-Length': chunksize,
    'Content-Type': 'video/mp4',
  })
  .send(str);

Really what you have is also correct, but it sounds like you just needed to add the .code(206) to the reply.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83