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

227
Visualizações
What is the order of execution of instruction in creating server in node.js?

Can anyone explain which instruction is getting executed first. http.createServer() or server.listen and when callback function inside createserver will execute?

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you look at the NodeJS documentation for the HTTP module here: https://nodejs.org/api/http.html#httpcreateserveroptions-requestlistener

You'll see what the http.createServer call returns, from the docs:

Returns a new instance of http.Server.

Thus the createServer call must complete before it can return a server object. Which is being used in your the server.listen call being made a few lines below. Asynchronous in NodeJS doesn't mean random.

about 4 years ago · Juan Pablo Isaza Relatório

0

http.createServer() is the first to execute because the variable server must be set before use to avoid undefined error. And the http.createServer is a synchrone function asynchronous-vs-synchronous

about 4 years ago · Juan Pablo Isaza Relatório
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