Sending PDF to Frontend from Node, without Express

I have a Node.js backend that I need to use to send a file in a response to the frontend. This Node backend doesn't use express, so I'm wondering what I can use to return the file as part of my response. This is the first time I'm come across a need to return an actual file rather than just data. The communication between the frontend and backend is handled via socket.io.

I have successfully written the code to generate the PDF - that is working:

async generateNotesPDF(args, session, ctx) {
  pdf.create(pdfTemplate(args), {}).toFile("result.pdf", err => {
    if (err) {
      return Response.Failure("Unable to generate PDF by parameters passed.");
    }

    return Response.Success("PDF was generated successfully.");
  });
}

Now, with Express I could do something like this to send the created PDF to the frontend:

async getNotesPDF(req, res) {
   return Response.Success("PDF was retreived successfully.", 
   res.sendFile(`${__dirname}/result.pdf`));
}

But as I say, I'm not sure how I would return the file as part of the response with socket.io. Is my best option to add a library? If so, which one is recommended for this kind of case? Or can socket.io handle this?

So far, every single example I've seen regarding sending files with socket.io is referencing uploading files from the frontend, as opposed to sending from the backend TO the frontend, which is what I need to do.



Read more here: https://stackoverflow.com/questions/66280442/sending-pdf-to-frontend-from-node-without-express

Content Attribution

This content was originally published by Muirik at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: