1

每当我重新加载页面时,程序中的错误消息都会记录在输出终端上。我不明白为什么。

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.......');
})
4

1 回答 1

1

将 a 添加console.log(req.url)else分支将有助于诊断问题。

当您通过浏览器访问网页时,它会尝试在/favico.ico路径(即,在本例中为http://localhost:5000/favico.ico)中查找图标,该图标将转到else您的代码分支并产生错误。如果您使用类似curlwget访问的东西localhost:5000,您将获得“主页”字符串而不会产生错误。

于 2021-12-14T20:17:58.150 回答