web_fullstack

node.js status code set

AI Coder 2020. 7. 29. 06:56

아래와 같이 코드를 짜고, 웹에서 http://localhost:3000/abc 와 같이 선언 되지 않은 page를 입력하면, 

page not found 가 회면에 나타난다. 

 

const http = require('http')

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

    if(req.url==='/about')

        res.end('The about page')

    else if(req.url==='/contact')

        res.end('The contact page')

    else if(req.url==='/')

        res.end('The home page')

    else{

        res.writeHead(404)

        res.end('page not found')

    }

})

server.listen(3000)

 

그리고 Chrome 의 More tools - Developer Tools 에 가서 보면, res.writeHead(404) 라고 입력한 status 가 보인다.