Search code examples
hapi.js

Is it possible to manually set the content-type header in reply.file()?


I have a file that I would like to serve from my local filesystem. That file, however, is not named according to naming conventions. Instead, I have stored some metadata elsewhere (in my database) about that particular file. What I'd like to do is say something like

reply.file(req.pre.item.actual_filename,
  {
    filename: req.pre.item.user_filename,
    mode: 'inline',
    'content-type': req.pre.item.mime_type
  });

It seems like hapijs just keeps saying 'octet stream' no matter how hard I try to convince it. I could store all my files in the local system with extensions, but that's not really what I want to do. I'd rather have hapi reply with the proper filetype.


Solution

  • I think this is a bug in hapi. The following should work:

    reply.file(req.pre.item.actual_filename,
    {
      filename: req.pre.item.user_filename,
      mode: 'inline'
    }).type(req.pre.item.mime_type);
    

    I submitted a pull request to fix this issue (#1956). Will update this answer when the pull request gets accepted and released.

    EDIT: The change was accepted, it will be in the 6.9.0 release.

    EDIT 2: Hapi 6.9.0 has been released with this change in it.