๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
๋ฐฑ์๋ express์ node.js ์ฌ์ฉํ๊ธฐ (3) ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
๋ฐฑ์๋ express์ node.js ์ฌ์ฉํ๊ธฐ (3)
์ง์ง์ํ์นด 2023. 1. 20. 15:54728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ ์ฐ๋ฆฌ๋ฐ ๋์ [Node.js] ๋ฐฑ์๋ ๋ง๋ณด๊ธฐ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค>
https://www.youtube.com/watch?v=AZtyyeCwNoc&list=PLSK4WsJ8JS4cQ-niGNum4bkK_THHOizTs&index=5
๐จ html ์๋ ์์ฑ
html ํ์ผ์
! + enter ๋๋ฅด๋ฉด ์๋์ผ๋ก html ๊ตฌ์กฐ ์ ๋ ฅ๋จ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
โญ ๋ก๊ทธ์ธ ํ๋ฉด (๋งค์ฐ ๊ตฌ์! ํ๋์ฝ๋ฉ)
// 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๋ก ๊ฐ๋๋ ์๋ฒ")
// })
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
์ฌ๊ธฐ๋ ๋ฃจํธ์
๋๋ค
</body>
</html>
`
);
});
app.get("/login", (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" placeholder = "์์ด๋"><br>
<input type="text" placeholder = "๋น๋ฐ๋ฒํธ"><br>
<button>๋ก๊ทธ์ธ</button>
</body>
</html>
`
);
});
app.listen(3000, function () {
console.log("์๋ฒ ๊ฐ๋");
});
โญ ๋ก๊ทธ์ธ ํ๋ฉด (EJS ์ฌ์ฉ)
๊ทธ ์ ์ ejs ์ค์นํ๊ธฐ
npm install ejs -s
views ํด๋ ๋ง๋ค๊ธฐ
login.ejs
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" placeholder = "์์ด๋"><br>
<input type="text" placeholder = "๋น๋ฐ๋ฒํธ"><br>
<button>๋ก๊ทธ์ธ</button>
</body>
</html>
index.ejs
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
์ฌ๊ธฐ๋ ๋ฃจํธ์
๋๋ค
</body>
</html>
app.js
const express = require("express");
const app = express();
// ์ฑ ์ธํ
app.set("views", "./views");
app.set("view engine", "ejs");
app.get("/", (req, res) => {
res.render("home/index");
});
app.get("/login", (req, res) => {
res.render("home/login");
});
app.listen(3000, function () {
console.log("์๋ฒ ๊ฐ๋");
});
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MVC ํจํด๊ณผ REST API ์๋ ์๋ฆฌ (0) | 2023.02.03 |
---|---|
๋ฐฑ์๋ express์ node.js ์ฌ์ฉํ๊ธฐ + Router (4) (0) | 2023.01.20 |
๋ฐฑ์๋ express์ node.js ์ฌ์ฉํ๊ธฐ (2) (0) | 2023.01.20 |
๋ฐฑ์๋ express์ node.js ์ฌ์ฉํ๊ธฐ (1) (0) | 2023.01.20 |
๋ค์ ๋์ .. Flask-SocketIO ์ฌ์ฉํด์ live-graph ๋ง๋ค๊ธฐ (8) (1) | 2022.12.02 |
Comments