📦express_practice
┣ 📂public
┃ ┣ 📂css
┃ ┃ ┗ 📜index.css
┃ ┗ 📂js
┃ ┃ ┗ 📜index.js
┣ 📂views
┃ ┣ 📜index.html
┃ ┣ 📜join.html
┃ ┣ 📜login.html
┃ ┗ 📜welcome.html
┣ 📜.gitignore
┣ 📜package-lock.json
┣ 📜package.json
┗ 📜server.js
server.js
app.get("/", (req, res) => {
const user = app.get("login");
if (user !== undefined) {
const nickname = user["nickname"];
res.render("index", { nickname: `${nickname}님 안녕하세요` });
} else {
res.render("index");
}
});
app.post("/join", (req, res) => {
const { id, pw, nickname } = req.body;
app.set("login", { id, pw, nickname });
user.push({ id, pw, nickname });
res.redirect("/welcome");
});
app.get("/welcome", (req, res) => {
const login = app.get("login");
app.set("login", undefined);
res.render("welcome", login);
});
const isIdPwCorrect = (id, pw) => {
for (let i = 0; i < user.length; i++) {
if (user[i]["id"] === id && user[i]["pw"] === pw) {
app.set("login", { id: user[i]["id"], pw: user[i]["pw"], nickname: user[i]["nickname"] });
return true;
}
return false;
}
};
app.post("/login", (req, res) => {
const { id, pw } = req.body;
if (isIdPwCorrect(id, pw)) {
res.redirect("/");
} else {
res.render("login");
}
});