1

Whenever I reload my page, the error message in the program is logged on the output terminal. I can't understand why.

const http = require('http')
const server = http.createServer((req,res)=>{

    if(req.url === '/'){
        res.end('Home Page')
        return;
    }
    else if(req.url === '/about'){
        res.end('About Page')
        return;
    }else{
        
        res.end('Error page')
        console.log('Error page encountered!');//?
        
    }
})

server.listen(5000,()=>{
    console.log('Server listening to port 5000.......');
})
Mureinik
  • 297,002
  • 52
  • 306
  • 350
kkk
  • 11
  • 2

1 Answers1

1

Adding a console.log(req.url) to the else branch would help diagnose the issue.

When you access a webpage via a browser, it attempts to look for a favicon at the /favico.ico path (i.e., in this case, http://localhost:5000/favico.ico), which will go to the else branch of your code and produce the error. If you use something like curl or wget to access localhost:5000, you'll get "Home Page" string without producing the error.

Mureinik
  • 297,002
  • 52
  • 306
  • 350