๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[http ๋ชจ๋๋ก ์๋ฒ] (1) http ๋ชจ๋๋ก ์น ๋ธ๋ผ์ฐ์ ์์ฒญํ๊ธฐ ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
[http ๋ชจ๋๋ก ์๋ฒ] (1) http ๋ชจ๋๋ก ์น ๋ธ๋ผ์ฐ์ ์์ฒญํ๊ธฐ
์ง์ง์ํ์นด 2023. 4. 16. 00:54728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ Node.js ๊ต๊ณผ์๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
๐ http ๋ชจ๋๋ก ์น ๋ธ๋ผ์ฐ์ ์์ฒญํ๊ธฐ
// http : ์น ๋ธ๋ผ์ฐ์ ์ ์์ฒญ ์ฒ๋ฆฌ
const http = require("http");
// ์์ฒญ์ ๋ํ ์ฝ๋ฐฑ ํจ์
http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.write("<h1>Hello node!</h1>");
res.end("<p>Hello Server!</p>");
})
.listen(8080, () => {
console.log("8080๋ฒ ํฌํธ์์ ์๋ฒ ๋๊ธฐ์ค");
})
๐ http ๋ชจ๋๋ก ์น ๋ธ๋ผ์ฐ์ ์์ฒญํ๊ธฐ + listening ์ด๋ฒคํธ ๋ฆฌ์ค๋
// http : ์น ๋ธ๋ผ์ฐ์ ์ ์์ฒญ ์ฒ๋ฆฌ
const http = require("http");
// ์์ฒญ์ ๋ํ ์ฝ๋ฐฑ ํจ์
http.createServer((req, res) => {
// writeHead : ์๋ต์ ๋ํ ์ ๋ณด๋ฅผ ๊ธฐ๋กํ๋ ๋ฉ์๋
// 1) 200 : ์ฑ๊ณต ์์ฒญ ๋ฉ์๋
// 2) ์๋ต์ ๋ํ ์ ๋ณด๋ฅผ ๋ณด๋ด๋๋ฐ ์ฝํ
์ธ ํ์์ด HTML
// 3) ํ๊ธ ํ์๋ฅผ ์ํด utf-8 ์ง์
// ์ ์ธ๊ฐ์ง์ ์ ๋ณด ๊ธฐ๋ก๋๋ ๋ถ๋ถ์ ํค๋
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
// ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ผ ๋ฐ์ดํฐ
// ๋ฐ์ดํฐ ๊ธฐ๋ก๋๋ ๋ถ๋ถ์ ๋ณธ๋ฌธ
res.write("<h1>Hello node!</h1>");
// ์๋ต์ ์ข
ํ๋ ๋ฉ์๋
res.end("<p>Hello Server!</p>");
})
server.listen(8080);
server.on("listening", () => {
console.log("8080๋ฒ ํฌํธ์์ ์๋ฒ ๋๊ธฐ์ค");
});
server.on("error", (error) => {
console.log(error);
})
๐ฟ localhost
: ํ์ฌ ์ปดํจํฐ์ ๋ด๋ถ ์ฃผ์
: ์์ ์ ์ปดํจํฐ์์๋ง ์ ๊ทผ ๊ฐ๋ฅ
: 127.0.0.1 ๋ก๋ ๊ฐ๋ฅ
: ์ซ์ ์ฃผ์๋ฅผ IP (Internet Protocol)
๐ฟ port
: ์๋ฒ ๋ด์์ ํ๋ก์ธ์ค๋ฅผ ๊ตฌ๋ถํ๋ ๋ฒํธ
: 80๋ฒ ํฌํธ ์ฌ์ฉํ๋ฉด http ์์ ํฌํธ ์๋ต ๊ฐ๋ฅ
: 443๋ฒ ํฌํธ๋ https ์์ ํฌํธ ์๋ต ๊ฐ๋ฅ
: ๋ฆฌ๋ ์ค์์๋ 1024 ์ดํ์ ํฌํธ์๋ ๊ด๋ฆฌ์ ๊ถํ ํ์
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments