0

I am trying to use the fastify-multer plugin to upload files to the server and I am able to successfully get the images uploaded to the folder. The problem is my app crashes. I used the fastify-cli generated structure and I am running it as a standalone server as mentioned in the README.md here.

I am writing it as a fastify plugin.

"use strict";

const fp = require("fastify-plugin");

module.exports = fp(function(fastify, opts, next) {
  fastify.decorate("uploadImage", function(request) {
    const path = require("path");
    const multer = require("fastify-multer");

    var storage = multer.diskStorage({
      destination: path.join(path.join(__dirname + "/uploads/")),
      filename: function(request, file, cb) {
        cb(null, file.originalname);
      }
    });

    var upload = multer({ storage }).single("myImage");
    upload(request, function(err) {
      if (err) {
        console.log(err);
      } else {
        console.log("Saved...");
        return { saved: true };
      }
    });
  });
  next();
});

And here is the error I get : enter image description here

1 Answers1

2

Hi looked into your issue. You are using fastify-multer in the wrong way. Invoking multer({ storage }).single("myImage") you are creating a fastify's preHandler hook that accepts 3 specific parameters. You can find more on the offical documentation. A simple working example could be the one you can see at fastify-multer:

const server = fastify()
// register fastify content parser
server.register(multer.contentParser)

server.route({
  method: 'POST',
  url: '/profile',
  preHandler: upload.single('avatar'),
  handler: function(request, reply) {
    // request.file is the `avatar` file
    // request.body will hold the text fields, if there were any
    reply.code(200).send('SUCCESS')
  }
})

If you need more help just provide me a repro repo on github and I'll try to figure out what is the best solution for your case.

Let me know! :)

Maksim
  • 215
  • 2
  • 12
  • The app I am developing needs to upload a lot of images in different routes. I want to use it as a fastify plugin so that I can just reuse it everywhere. How do I achieve that ? – Ehtesham Siddiqui Jun 30 '19 at 13:13
  • It is already ready to do that. This plugin exposes a preHandler that you can use in every route you want. You just need to pass it as preHandler hook on that specific route. You don't need to add another plugin. – Maksim Jul 01 '19 at 09:28
  • Hey that worked absolutely fine! I want to run auth check in preHandler and then only upload the files. Could you suggest a way of doing it ? – Ehtesham Siddiqui Jul 01 '19 at 17:08
  • 1
    Of course. `preHandler` could also be an array of handlers: preHandler: [authenticationPreHandler, upload.single('avatar')]. I suggest you [fastify-auth](https://github.com/fastify/fastify-auth) – Maksim Jul 02 '19 at 08:08
  • In nginx unit your example get /fastify-multer/lib/lib/make-prehandler.js:174 request.req.pipe(busboy); ^ TypeError: request.req.pipe is not a function. What can be wrong? – Alexufo Sep 20 '19 at 23:44
  • What do you mean with "In nginx unit" and can you add a repro repository? – Maksim Sep 22 '19 at 09:35
  • @Maksim https://github.com/nginx/unit/issues/317 it was an issue. Pipes does not supporting now in nginx unit :-( – Alexufo Sep 29 '19 at 11:58