0

I want to simulate an upload in Node.js REST api. I want to make the REST api upload the files to a directory and then save the URL of the saved file to the database table. For some reason, I dont know of , he just tells me , "No Files uploaded" But looking thru, everything seems okay.

I am testing thru Postman, so thats where i seem to be getting the output and what it tells me.

My code is looking like this:

app.post('/api/createpost', function (req,res){
    if(!req.files) return res.status(400).send('No Files uploaded');
    
    const {foo} = req.files;
    const uploadTo = `postimages/${foo.name}`;
    
    foo.mv(uploadTo, (err) =>{
    if(err) return res.status(500).send(err);
        
    //push data to mysql Db
    
    var username = req.body.username;
    var imgUrl = 'http://localhost/'+uploadTo;
    var post = req.body.post;
    
    dbConn.query('INSERT INTO instagramclonedbposts SET ?', [username, imgUrl, post], function(error, results, fields){
        if(error) throw error;
        return res.send({error:false, message: 'Post Created'});
        });
    
    });
});

Testing in Postman, I have this :

node-Postman.png

Could there be something i am missing?

James Z
  • 12,209
  • 10
  • 24
  • 44
Blaq
  • 53
  • 2
  • 10

0 Answers0