Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

139
Visualizações
How to handle promise return inside NodeJs HTTP?

Using Cpanel, i setup a NodeJs server app as follows (mostly from the default template except for the GetSignedMessage() i included.

Users will GET a signed message from this server which returns a signed message.

var http = require('http')

async function GetSignedMessage() {
  return someSigningFunction(secret); // this returns a Promise<string>
}

var server = http.createServer(function(req, res) {

  if (req.method === "GET") {
    res.writeHead(200, {'Content-Type': 'text/plain'});

    var message = 'It works!';
    var message2 = GetSignedMessage();
    response = [message, message2].join('\n');

    res.end(response);
   }
});

server.listen();

Result when accessing the URL:

It works!
[object Promise]

How can I make the [object Promise] show as a string?

i know i should do something like GetSignedMessage().then(y => console.log(y)) where y appears as the string, but how do i store y inside message2 so it can appear in the response?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda