๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

๋ฐฑ์—”๋“œ express์™€ node.js ์‚ฌ์šฉํ•˜๊ธฐ (2) ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฐฑ์—”๋“œ(Back-End)/Node js

๋ฐฑ์—”๋“œ express์™€ node.js ์‚ฌ์šฉํ•˜๊ธฐ (2)

์ง•์ง•์•ŒํŒŒ์นด 2023. 1. 20. 15:07
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ์šฐ๋ฆฌ๋ฐ‹ ๋‹˜์˜ [Node.js] ๋ฐฑ์—”๋“œ ๋ง›๋ณด๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค>

https://www.youtube.com/watch?v=7gF09WFGK4I&list=PLSK4WsJ8JS4cQ-niGNum4bkK_THHOizTs&index=4

 

โญ http๋กœ ์„œ๋ฒ„ ๊ฐ€๋™

const http = require("http");
const app = http.createServer((req, res) => {
    console.log(req.url);
});

app.listen(3001, () => {
    console.log("http๋กœ ๊ฐ€๋™๋œ ์„œ๋ฒ„")
})

\

 

 

 

โญ if ๋ฌธ์œผ๋กœ ์„œ๋ฒ„ ์ œ์–ด (ํ•œ๊ธ€ ๊นจ์ง)

const http = require("http");
const app = http.createServer((req, res) => {
    if (req.url === "/") {
        res.end("์—ฌ๊ธฐ๋Š” ๋ฃจํŠธ");
    } else if (res.url === "/login") {
        res.end("์—ฌ๊ธฐ๋Š” ๋กœ๊ทธ์ธ");
    }
});

app.listen(3001, () => {
    console.log("http๋กœ ๊ฐ€๋™๋œ ์„œ๋ฒ„")
})

 

 

โญ if ๋ฌธ์œผ๋กœ ์„œ๋ฒ„ ์ œ์–ด (ํ•œ๊ธ€ ๊ฐ€๋Šฅ)

writeHead ์‚ฌ์šฉ

const http = require("http");
const app = http.createServer((req, res) => {
    // ํ•œ๊ธ€๋กœ ์ฝ์–ด์คŒ
    res.writeHead(200, {"Content-Type" : "text/html; charset=utf-8" });

    if (req.url === "/") {
        res.end("์—ฌ๊ธฐ๋Š” ๋ฃจํŠธ");
    } else if (req.url === "/login") {
        res.end("์—ฌ๊ธฐ๋Š” ๋กœ๊ทธ์ธ");
    }
});

app.listen(3001, () => {
    console.log("http๋กœ ๊ฐ€๋™๋œ ์„œ๋ฒ„")
})

728x90
๋ฐ˜์‘ํ˜•
Comments