Managing uploads and downloads
We need to add two more functionalities to our application, and we will do it by developing a dedicated Fastify plugin. The first will allow our users to upload CSV files to create to-do tasks in bulk. We will rely on two external dependencies to do it:
@fastify/multipart
for file uploadscsv-parse
for CSV parsing
The second plugin will expose an endpoint to download items as a CSV file. Again, we need the external csv-stringify
library to serialize objects and create the document.
While we will split the code into two snippets in the book, the complete code can be found in ./routes/todos/files/routes.js
. Let’s explore the following snippet, which contains the file upload and items bulk creation logic:
const fastifyMultipart = require('@fastify/multipart') const { parse: csvParse } = require('csv-parse') // ... omitted for brevity await fastify.register(fastifyMultipart, { // [1] &...