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

[Login & Register authentication with Node js] (2) ejs๋กœ html ํ™”๋ฉด ๋„์šฐ๊ธฐ ๋ณธ๋ฌธ

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

[Login & Register authentication with Node js] (2) ejs๋กœ html ํ™”๋ฉด ๋„์šฐ๊ธฐ

์ง•์ง•์•ŒํŒŒ์นด 2023. 3. 14. 23:43
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” Traversy Media ์˜ ์œ ํŠœ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

=> Node.js With Passport Authentication | Full Project

=> authentication app with login, register and access control using Node.js, Express, Passport, Mongoose

 

 

๐Ÿฅ• ejs (Embedded JavaScript) 

: JavaScript๋กœ HTML ๋งˆํฌ ์—…์„ ์ƒ์„ฑ ํ•  ์ˆ˜์žˆ๋Š” ๊ฐ„๋‹จํ•œ ํ…œํ”Œ๋ฆฟ ์–ธ์–ด ( view๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์–ธ์–ด )

์ฐธ๊ณ  => https://noodler.tistory.com/26

 

<% %>

ํ๋ฆ„ ์ œ์–ด๋ฌธ 

<%=  %>

๋ณ€์ˆ˜ ๊ฐ’

<%-  %>

์˜ˆ์•ฝ์–ด

<%- include('view์˜ ์ƒ๋Œ€์ฃผ์†Œ') %>

๋‹ค๋ฅธ viewํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ ์˜ด

 

๐Ÿฅ• ejs ์ฐธ๊ณ  ์‚ฌ์ดํŠธ (bootstrap.min.css)

https://bootswatch.com/

 

Bootswatch: Free themes for Bootstrap

Customizable Changes are contained in just two SASS files, enabling further customization and ensuring forward compatibility.

bootswatch.com

 

๐Ÿฅ• font ์ฐธ๊ณ  ์‚ฌ์ดํŠธ (css)

https://fontawesome.com/

\

 

๐Ÿฅ• bootstrap ์ฐธ๊ณ  ์‚ฌ์ดํŠธ (min.js)

https://getbootstrap.com/docs/5.3/getting-started/introduction/

 

 

๐Ÿฅ• "welcome" ejs ํŽ˜์ด์ง€ ๋„์šฐ๊ธฐ

๐Ÿง app.js

const express = require("express");
const expressLayouts = require("express-ejs-layouts");

const app = express();

// ejs ๋ฏธ๋“ค์›จ์–ด
app.use(expressLayouts);
// express ์˜ view ์—”์ง„์„ ejs ๋กœ ์„ธํŒ…
app.set("view engine", "ejs");
//  set ์„ ํ•œ ๋ถ€๋ถ„์€ layout ํด๋”์˜ layout ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์„ ์–ธ
// Routes
app.use("/", require("./routes/index"));
app.use("/users", require("./routes/user"));

const PORT = process.env.PORT || 8000;

app.listen(PORT, console.log(`๐Ÿš€Server started on port http://localhost:${PORT}`));

 

๐Ÿง routes/index.js

const express = require("express");
const router = express.Router();

// Main page
router.get("/", (req, res) => {
    // rednering ํ•  ๋•Œ view์˜ ํŒŒ์ผ ์ด๋ฆ„ ์จ์•ผ๋จ
    res.render("welcome");
});

module.exports = router;

 

๐Ÿง views/layout.ejs

<!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">
  <link rel="stylesheet" href="https://kit.fontawesome.com/f87852c84c.css" crossorigin="anonymous">
  <link rel="stylesheet" href="https://bootswatch.com/4/journal/bootstrap.min.css">
  <title>Node js๋กœ ๋กœ๊ทธ์ธ ๊ตฌํ˜„ํ•˜๊ธฐ</title>
</head>

<body>
  <div class="container">
    <%- body %>
  </div>



  <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"
    integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3"
    crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.min.js"
    integrity="sha384-mQ93GR66B00ZXjt0YO5KlohRA5SY2XofN4zfuZxLkoj1gXtW8ANNCe9d5Y3eG5eD"
    crossorigin="anonymous"></script>
</body>

</html>

 

๐Ÿง views/welcome.ejs

<h1>Welcome</h1>

 

์•„๋งˆ 1๋ฒˆ ๋”ฐ๋ผํ•˜์‹  ๋ถ„๋“ค์€ ejs ๋ชจ๋“ˆ์ด ์—†๋‹ค๊ณ  ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ž„

npm install ejs --save ํ•˜์‹œ๋ฉด ๋จ

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