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

95
Visualizações
Local Host is not sending any data

Whenever i ran this node server on Port "8000"

const http=require('https')
const PORT=8000

const server=http.createServer()

server.on('request',(req,res)=>{

    if (req.url==='friend'){

        res.writeHead(200,{
            'Content-Type':'text/plain',
        })
    
        res.end(JSON.stringify({
            id:1,
            name:'Sir Issac Newton',
        }))

    }

    if (req.url==='foe'){
        res.writeHead(200,{
            'Content-Type':'text/plain',
        })

        res.end('The Enemy is Ego Bro')
    }


})


server.listen(PORT,()=>{
    console.log(`the respnse is exectued at ${PORT}`)
})

i get an error on the Borwser which says:

localhost didn’t send any data. ERR_EMPTY_RESPONSE

i try to change ports but still it shows this error.Please what should i do and explain me what this error is. Thank you!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This code has 3 problems.

  1. You should change const http=require('https') to const http=require('http'). If you want to use HTTPS please see the nodejs document for how to configure the https server

  2. In nodejs HTTP request URL start with / and your condition statement does not work

  3. Cuz request URL does not match with the condition statement server does not respond any things and this error happens.

You should change code like that:

const http=require('http')
const PORT=8000

const server=http.createServer()

server.on('request',(req,res)=>{

    if (req.url==='/friend'){

        res.writeHead(200,{
            'Content-Type':'text/plain',
        });
    
        res.end(JSON.stringify({
            id:1,
            name:'Sir Issac Newton',
        }));

       return;
    }

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

        res.end('The Enemy is Ego Bro');

        return;
    }

    res.writeHead(400,{
        'Content-Type':'text/plain',
    });

    res.end('URL not match');
})


server.listen(PORT,()=>{
    console.log(`the respnse is exectued at ${PORT}`)
})
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