S
S
sasha-ld2021-08-21 11:50:10
Node.js
sasha-ld, 2021-08-21 11:50:10

Nodejs how to write server response to variable?

I make a request to the server, how to correctly write the response to a variable?

let str;
  const net = require('net');
  const client = net.createConnection({
    host: '127.0.0.1',
    port: 3545
    }, () => {
    client.write('status\r\n');
  });
  client.on('data', (data) => {
    str = data;
    client.end();
  });
  client.on('end', () => {
  });

console.log(str)


I understand that console.log will output undefined since it will be executed before the result is written to the variable, but how to do it right?

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