0

I was facing this tiny issue in a sample NodeJS application I was creating, that I always receive req.files as undefined even though I am uploading a file via postman. Here's the code:

import express from 'express';
import http from 'http';


let app = express();


app.post('/', function(req, res) {
    let files = req.files;
    res.send(files);
})

app.listen(process.env.PORT || 8000, () => {
    console.log(`Started on port 8000`);
});
  
export default app;

And here's how I am sending the file via postman: enter image description here

But I still get and empty response and also as logging on cmd I get the output as undefined: enter image description here

Would be greatful if anyone could help.

Community
  • 1
  • 1
Trishant Pahwa
  • 2,559
  • 2
  • 14
  • 31

1 Answers1

3

If you want to upload files, I recommend using multer: https://www.npmjs.com/package/multer

But if you don't want to use multer or bodyparser, you can stream the data:

req.on('data', data => {
      console.log(data);
    });

You can pipe the stream to write to file. check fs documentation.

Abhyudit Jain
  • 3,640
  • 2
  • 24
  • 32