Node.js
[Node.js] 노드제이에스 - 라우팅
Seong-Jun
2024. 2. 5. 11:56
728x90
반응형
SMALL
Do it! Node.js 프로그래밍 입문 공부단 3일차 입니다.
라우팅
웹 프로그래밍에서 라우팅이란 클라이언트에서 들어오는 요청에 따라 그에 맞는 함수를 실행하는 것을 말합니다. 라우팅을 이용하면 사용자가 입력하는 url에 따라 다른 내용을 보여줄 수 있습니다. 또한 GET이나 POST, PUT, DELETE 같은 요청 메서드에 따라 처리할 함수를 다르게 연결할 수 있습니다.
const http = require("http")
// 라우팅
const server = http.createServer((req, res) => {
const {method, url} = req
res.setHeader("Content-Type", "text/plain")
if(method === "GET" && url === "/home") {
res.statusCode = 200
res.end("HOME")
} else if(method === "GET" && url === "/about") {
res.statusCode = 200
res.end("ABOUT")
} else {
res.statusCode = 404
res.end("Not Found")
}
})
server.listen(3000, () => {
console.log("3000번 포트에서 서버 실행")
})
728x90
반응형
LIST