0

i having some issues, Cannot set headers after they are sent to the client. This is my back-end side error afterpost request.It looks like problem with headers

this my code

 app.post('/upload', function (req, res) { //post data into databases
      var data = { PhotoName: 'http://localhost:8000/public/'+req.body.filename+'.jpg'};
        var sql = 'insert into photo set ?';
        db.query(sql, data, (err, result) => {
            if (err) throw err;
            console.log(result);
            res.send({
                type: 'POST',
              PhotoName:'http://localhost:8000/public/'+req.body.filename + '.jpg'
            });    
        });


      let imageFile = req.files.file;

      imageFile.mv(`${__dirname}/public/${req.body.filename}.jpg`, function (err) {
        if (err) {
          return res.status(500).send(err);
        }
        res.json({ file: `public/${req.body.filename}.jpg` });
      });

    });

thank you very much

ghassan
  • 45
  • 2
  • 7
  • Possible duplicate of [Error: Can't set headers after they are sent to the client](https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client) – The Impaler May 24 '18 at 14:56

1 Answers1

0

That's right. You cannot set HTTP headers once the HTTP body response has begun to be sent. That's kind of the definition of HTTP.

What part you disagree with?

So... set the headers before you build the response.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • There are many ways of setting headers. For example the line **return res.status(500).send(err);** is doing it... and it's executed somewhat after the other lines. Maybe you should switch the order of those commands. I recommend studying HTTP in more detail. – The Impaler May 24 '18 at 14:59