U
U
Umd232021-02-16 22:43:18
Node.js
Umd23, 2021-02-16 22:43:18

How to send a string to a client?

Hey! On the server, I make a post request handler, everything turns out quite successfully, but I ran into an incomprehensible garbage. I can not send a response (which I can easily see in the console)

app.post('/task', function(req, res) {
    var id = req.body.id;

var https = require('follow-redirects').https;
var fs = require('fs');

var options = {
  'method': 'POST',
  'hostname': 'hostname',
  'maxRedirects': 20
};

var req = https.request(options, function (res) {
  var chunks = [];

  res.on("data", function (chunk) {
    chunks.push(chunk);
  });
  res.on("end", function (chunk) {
    let body = Buffer.concat(chunks);
    urlsend = String(body);
   console.log(urlsend);  //Вот тут спокойно выводится в консоль все, что меня интересует
  });
  res.on("error", function (error) {
    console.error(error);
  });
});

var postData =  "postData";
req.write(postData);
req.end();
res.send(String(global.urlsend)); //А тут с помощью глобального объекта String не хочет выводить инфу. С помощью toString тоже не получается, ругается ошибкой. 
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question