J
J
judywb2022-03-30 10:35:51
JavaScript
judywb, 2022-03-30 10:35:51

Why is req.files undefined?

I'm trying to get an image and put it in static, but for some reason, when I use form-data in postman, it does not see either req.body or req.files, although I exactly repeated the guide.
The code:

const express = require('express')
const fileUpload = require('express-fileupload');
const fileSevice = require('./fileSevice')
const app = express()

app.use(fileUpload())
app.use(express.json())
app.use(express.static('static'))

app.get('/', (req, res) => {
    console.log(req.body)
    const file = fileSevice.saveFile(req.files.picture)
    res.send(file)
})

app.listen(3000, () => console.log('SERVER STARTED ON PORT ' + 3000))


Screenshot from postman: Guide 6244080d605e0114246448.png

: https://github.com/utimur/backend-for-kids-course/... , or rather req.files in postController.
Another resource that supposedly has a solution to the problem: https://thewebdev.info/2021/07/04/how-to-fix-the-r...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wonderingpeanut, 2022-03-30
@wonderingpeanut

firstly, you need to send not via form-data, but via x-www-form-urlencoded
secondly, you need to add the urlencoded middleware:
app.use(express.urlencoded({ extended: false });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question