Node-js-3
Node-js - Log in ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
๋ก๊ทธ์ธ์์ ์์ด๋, ํจ์ค์๋๋ฅผ ๋ฐ์์ ๋, ์๋ฒ์์ ์ ๋ ฅ๊ฐ ๋น๊ต, ์ฒ๋ฆฌ DOM, fetch, Restful API bodyParser, .then โ
DOM์ผ๋ก HTML ๊ฐ์ฒด ์ ์ดํ๊ธฐ | ํ๋ฐํธ ๊ธฐ๋ฅ ๊ตฌํ
๋ก๊ทธ์ธ ํ๋ฉด์์ ์์ด๋์ ๋น๋ฐ๋ฒํธ ์ ๋ณด๋ฅผ โ๋ก๊ทธ์ธโ ๋ฒํผ ๋๋ฅผ ๋, ์๋ฒ๋ก ์ฎ๊ธฐ๋ ์์
DOM์ ์ด์ฉํด์ ํ๋ค. (document.queryselector) html ๊ฐ์ ์ด์ฉํ๋ค.
js์์ ejs ๋ด์ ์๋ ๊ฐ์ ์ ๊ทผ ๋ถ๊ฐ.. ์๋ํ๋ฉด js๊ฐ ๋จผ์ ์คํ๋๊ธฐ ๋๋ฌธ์ด๋ค.
-> script์ โdeferโ๋ฅผ ์ด์ฉํด์ ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ํ์. (async?)
<script src="/js/home/login.js" defer></script>
๋ก๊ทธ์ธ ๋ฒํผ ํด๋ฆญ ์ ์์ด๋์ ๋น๋ฐ๋ฒํธ๋ฅผ ๊ฐ์ฒด๋ก ๋ฐ๋๋ค.
fetch | ํ๋ฐํธ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
๊ฒฝ๋ก๋ ๋ณดํต ๋ฐฑ์๋ ๊ฐ๋ฐ์๊ฐ ์๋ฒ ๊ฒฝ๋ก ๊ฐ๋ฐ ๊ทธ๋ฆฌ๊ณ ํด๋น ๊ฒฝ๋ก๋ฅผ ์์ฒญํ๋ ๋ฐ ๊ทธ ํด๋น ๊ฒฝ๋ก์๋ ์ด๋ฏธ API๊ฐ ๋ง๋ค์ด์ ธ ์์ด์ผ ํ๋ค.
API ๋ง๋ค์ด์ ธ์๋ค๋ ๊ฐ์ ์ ํ๊ณ ์ ๋ฌ ๊ณผ์ ๊ฐ๋ฐ.
fetch๋ฅผ ํตํด ๋ก๊ทธ์ธ ๊ฒฝ๋ก์ ์ ๋ฌ์ ํด์ค๋ค.
fetch("/login", {
method: "POST", //restful API? -> ๋ฉ์๋ ์ด์ฉ
header: {
"Content-Type": "apaplication/json",
},
body: JSON.stringify(req), // ๋ฐ์ดํฐ
});
body์ ๋ฐ์ดํฐ๋ฅผ ์ค์ด์ ๋ณด๋ด๋๋ฐ method์ header๊ฐ ํ์ํ๋ค.
-> method, header๋ RESTful API๋ฅผ ๋ฐ๋ฅธ๋ค.
REST API(RESTful API, ๋ ์คํธํ API)๋?
REST ์ํคํ
์ฒ์ ์ ์ฝ ์กฐ๊ฑด์ ์ค์ํ๋ ์ ํ๋ฆฌ์ผ์ด์
ํ๋ก๊ทธ๋๋ฐ ์ธํฐํ์ด์ค๋ฅผ ๋ปํฉ๋๋ค. REST๋ Representational State Transfer์ ์ค์๋ง์
๋๋ค.
API(Application Programming Interface)๋?
์ ํ๋ฆฌ์ผ์ด์
์ํํธ์จ์ด๋ฅผ ๊ตฌ์ถํ๊ณ ํตํฉํ๋ ์ ์ ๋ฐ ํ๋กํ ์ฝ ์ธํธ์
๋๋ค.
๋๋๋ก API๋ ์ ๋ณด ์ ๊ณต์์ ์ ๋ณด ์ฌ์ฉ์ ๊ฐ์ ๊ณ์ฝ์ผ๋ก ์ง์นญ๋๋ฉฐ ์๋น์์๊ฒ ํ์ํ ์ฝํ
์ธ (ํธ์ถ)์ ์์ฐ์์๊ฒ ํ์ํ ์ฝํ
์ธ (์๋ต)๋ฅผ ๊ตฌ์ฑํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋ ์จ ์๋น์ค์ฉ API์์๋ ์ฌ์ฉ์๋ ์ฐํธ๋ฒํธ๋ฅผ ์ ๊ณตํ๊ณ , ์์ฐ์๋ ๋ ๋ถ๋ถ(์ฒซ ๋ฒ์งธ๋ ์ต๊ณ ๊ธฐ์จ, ๋ ๋ฒ์งธ๋ ์ต์ ๊ธฐ์จ)์ผ๋ก ๊ตฌ์ฑ๋ ์๋ต์ผ๋ก ๋ตํ๋๋ก ์ง์ ํ ์ ์์ต๋๋ค.
์์ธํ ์ค๋ช
. ์ถ์ฒ:link
Rest : ์ฝ๊ฒ ๋งํ์๋ฉด, ์ฌ์ดํธ ๊ตฌ์ฑ ์๋ฆฌ, ์ฌ์ดํธ๋ฅผ ์ด๋ป๊ฒ ๊ตฌ์ฑํ ๊ฒ์ธ๊ฐ?
์ฆ, Restful API๋ Rest๋ฐฉ์์ ๋ฐ๋ฅด๋ API๋ฅผ ์๋ฏธ
(REpresntatiional : ํํ, State : ์ํ, Transter : ์ ์ก) : ์์์ ํํ์ ๊ฐ์ง๊ณ ์์์ ์ ์กํ๋ค.
์์(Resource) : URI
- /feed/subscriptions
ํํ(Representational) : Header ex) ๋ฐ๊ณ ์ถ์ ์๋ต ํ์ , ํด๋ผ์ด์ธํธ๊ฐ ์ ๋ฌํ๋ ์๋ต ํ์
- โContent-Typeโ : โtext/htmlโ, โimage/gifโ
- โAcceptโ : โtext/htmlโ, โimage/pngโ
์ํ(State) : Method (HTTP method)
- GET ์กฐํ, POST ์์ฑ, PUT ์์ , DELETE ์ญ์ , PATCH ์์์ ์ผ๋ถ ์์ ..etc
Restful API ์ค๊ณ ์์น : 1๋ฒ๋ง ์ค๊ณํด์ฃผ๋ฉด ๋๋ค. 2~6๋ฒ์ ์์์ ์ค๊ณ๊ฐ ๋์ด์๋ค.
- Uniform Interface
- StateLess
- Cacheable
- Code on demand
- Layered System
- Client / Server
๋ก๊ทธ์ธ API ๋ง๋ค๊ธฐ in ์๋ฒ | ํ๋ฐํธ์ ์์ฒญ๋ฐ์ดํฐ ํ์ฑ
rendering ํด์ฃผ๋ ํจ์๋ฅผ output ๊ฐ์ฒด๋ก ๋ฃ์ด ๋ถ๋ฆฌํด์ค๋ค.
const output = {
home: (req, res) => {
res.render("home/index");
},
login: (req, res) => {
res.render("home/login");
},
};
login.js์์ requestํด์ฃผ๋ ๋ฐ์ดํฐ๋ฅผ home.ctrl.js์์ ์ฒ๋ฆฌํ ์ ์๊ฒ ๋ง๋ค์ด์ฃผ๊ธฐ ๊ทธ๋ฆฌ๊ณ index.js์์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋์ง ๋ณธ๋ค.
login.js
function login() {
const req = {
id: id.value,
pwd: pwd.value,
};
fetch("/login", {
method: "POST", //restful API? -> ๋ฉ์๋ ์ด์ฉ
header: {
"Content-Type": "application/json",
},
body: JSON.stringify(req), // ๋ฐ์ดํฐ
});
}
home.ctrl.js
const process = {
login: (req, res) => {
console.log(req.body);
},
};
index.js
router.post("/login", ctrl.process.login);
ํ์ง๋ง ์์ง ๋ฐ์ ์ ์๋ค. -> body parsing ํด์ฃผ๋ ๋ชจ๋ ์ค์น
app.js
const bodyParser = require("body-parser");
terminal npm i body-parser -s ( -s : save๋ผ๋ ์๋ฏธ๋ก packages์์ dependencies์์ ๋ช ์๋๋ค. )
๊ทธ๋๋ ์๋๋ค. -> ๋ฏธ๋ค์จ์ด ๋ฑ๋กํด์ค์ผํ๋ค.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
-> deprecated ์ค๋ฅ : ์ฌ์ฉํ์ง ์๋๋ค๋ผ๋ ๋ป์ผ๋ก, express ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ํด๊ฒฐ๋๋ค.
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
**๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํ ๋, ์ ์ฅ๋ฒํผ ์ฐ์์ผ๋ก ๋๋ฅด์ง ๋ง๊ธฐ -> ์ด๊ฑฐ ๋๋ฌธ์ ์ ์ ์ค๋ฅ ์๊ธด๋ฏ ?
๋ก๊ทธ์ธ ์ธ์ฆ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ in ์๋ฒ | ์ ์ ๋ฐ์ดํฐ ๋ง๋ค๊ธฐ
๋ก๊ทธ์ธ ์์ ์๋ฒ์ ์๋ ์ ๋ณด์ ๋ก๊ทธ์ธ ์ ๋ณด๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ ํจ์๋ฅผ ๋ง๋ ๋ค.
ํ์ธํ users ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ req๊ฐ ์์ ๋ ์ด ์์ฒญ์ ์ฒ๋ฆฌํ๋ ๊ฐ์ฒด์ธ process์ user์ ๋น๊ตํ๋ ํจ์์ response๋ฅผ return ํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ค๋ค.
home.ctrl.js
const users = {
id: ["hyunsoo", "rlagustn", "gustn"],
pwd: ["1234", "5678", "9012"],
};
const process = {
login: (req, res) => {
const id = req.body.id,
pwd = req.body.pwd;
if (users.id.includes(id)) {
const idx = users.id.indexOf(id);
if (users.pwd[idx] === pwd) {
return res.json({
success: true,
});
}
}
return res.json({
success: false,
msg: "๋ก๊ทธ์ธ์ ์คํจํ์ต๋๋ค.",
});
},
};
res.json์ ํตํด ์๋ต์ ๋ ๋ ธ์ ๋, reqํ๋ ํจ์(fetch)์์ ์๋ตํจ์๋ฅผ ๋ฐ์ผ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด .then ๋งค์๋๋ฅผ ์ด์ฉํ๋ค.
login.js
fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(req),
}).then((res) => console.log(res.json()));
์ฌ๊ธฐ์ console์ ํตํด ์๋ตํจ์์ json๊ฐ์ด ๋์ฐฉํ๋๋ฐ, ๋ค์๊ณผ ๊ฐ์ด Promise ๊ฐ์ฒด๊ฐ ๋์ฐฉํ๊ฒ ๋๋ค.
์ค๋ช : res.json()์ ๋ฐํ ๊ฐ์ Promise๋ค. ๊ธฐ๋ณธ res์ ๋ฐํ ๊ฐ์ Response ์คํธ๋ฆผ์ธ๋ฐ, โ.json()โ ๋งค์๋๋ฅผ ํตํด Response(์๋ต) ์คํธ๋ฆผ์ ์ฝ์ ์ ์๋ค.
Response๋ ๋ฐ์ดํฐ๊ฐ ๋ชจ๋ ๋ฐ์์ง ์ํ๊ฐ ์๋๋ค. โ.json()โ์ผ๋ก Response ์คํธ๋ฆผ์ >๊ฐ์ ธ์ ์๋ฃ๋ ๋๊น์ง ์ฝ๋๋ค. ๋ค ์ฝ์ body์ ํ ์คํธ๋ฅผ Promise ํํ๋ก ๋ฐํํ๋ค.
-> ๊ฐ์ธ์ ์ผ๋ก ๊ณต๋ถํ๊ธฐ..
then์ ํ๋ฒ์จ์ res๋ฅผ ๋ณด์ด๊ฒ ํ๋ฉด ๊ฐ์ฒด ์์ ๊ฐ์ด ๋์ค๊ฒ๋๋ค.
.then((res) => res.json())
.then(console.log);
์๋ฒ์ ์๋ต๋ฐ์ดํฐ ์ฒ๋ฆฌ
thenํ ์๋ต ๊ฐ์ ๋ฐ๋ฅธ ์กฐ๊ฑด์ ์ค์ ํด์ค๋ค.
.then((res) => res.json())
.then((res) => {
if (res.success) {
location.href = "/";
} else {
alert(res.msg);
}
})
.catch((err) => {
console.error(new Error("๋ก๊ทธ์ธ ์ค ์๋ฌ ๋ฐ์"));
});