B
B
Bogdan2019-05-16 15:30:10
Node.js
Bogdan, 2019-05-16 15:30:10

Middleware before Multer?

Hello. Tell me please. How to do it right, is it possible somehow in middlware before multer to get the parameters to validate them (or create an entry in the database whose id will be the file name in multer)?

app.use(express.urlencoded({ extended: true }));

routes.post(
  '',
  async (req, res, next) => next(req.body.some_params ? null : new Error('MISSING_PARAMS')),
  multer({ dest: './uploads' }).single('file'),
  (req, res) => res.json({ status: true })
);

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-05-16
@Lynn

No way. In order for req.body to appear, you need to parse the request body. This is exactly what multer does.
As it says in the documentation for body-parser :
Just look in the same Chrome developer tools how the form sent in the multipart/form-data format looks like and you will probably understand why it is difficult to parse. And, generally speaking, the order of the fields can be such that the file will go first, and then the id you need.
In general, as I said, you just need to save the file to temporary storage, and then transfer it where necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question