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

[CRUD์„ ์ด์šฉํ•œ File upload Web] (7) router์— ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (multer) ์‹œํ‚ค๊ณ  ๋ฉ”์ธ ํ™ˆ์œผ๋กœ redirect ๋ณธ๋ฌธ

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

[CRUD์„ ์ด์šฉํ•œ File upload Web] (7) router์— ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (multer) ์‹œํ‚ค๊ณ  ๋ฉ”์ธ ํ™ˆ์œผ๋กœ redirect

์ง•์ง•์•ŒํŒŒ์นด 2023. 3. 10. 01:46
728x90
๋ฐ˜์‘ํ˜•

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

=> CRUD App With Image Upload Using NodeJs, ExpressJs, MongoDB & EJS Templating Engine

 

 

๐Ÿฅ• ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ (multer) ์‹œํ‚ค๊ณ  ๋ฉ”์ธ ํ™ˆ์œผ๋กœ redirect

๐Ÿง routes/routes.js

const express = require("express");
const router = express.Router();
const User = require("../models/user");
const multer = require("multer");

// image upload
var storage = multer.diskStorage({
  // ํŒŒ์ผ์ด ์—…๋กœ๋“œ๋  ๊ฒฝ๋กœ ์„ค์ •
  destination: function (req, file, cb) {
    cb(null, "./uploads");
  },
  // timestamp๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด ํŒŒ์ผ๋ช… ์„ค์ •
  filename: function (req, file, cb) {
    cb(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
  }
})

var upload = multer({
  storage: storage,
}).single("image");   // ํ•œ ๊ฐœ์˜ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ

// Insert an user into database route
router.post("/add", upload, (req, res) => {
  const user = new User({
    name: req.body.name,
    email: req.body.email,
    phone: req.body.phone,
    image: req.file.filename,
  });

  user.save().then((err) => {
    // index.js ์— res.locals.message ์žˆ์Œ
    req.session.message = {
      type: "success",
      message: "User added successsfull!"
    };
    // ์ง€์ •๋œ ๋‹ค๋ฅธ URL๋กœ ์žฌ์š”์ฒญ (ํ™ˆ)
    res.redirect("/");
  }).catch((err) => {
    res.json({ message: err.message, type: "danger" });
  })
});

// Home (ejs ์™€ ์—ฐ๋™)
router.get("/", (req, res) => {
  res.render("index", { title: "Home" });
});

router.get("/add", (req, res) => {
  res.render("addUsers", { title: "Add users" });
})

module.exports = router;

๋ฒ„ํŠผ ๋ˆ„๋ฅด๋ฉด ๋ฉ”์ธ ํ™ˆ์œผ๋กœ ๋Œ์•„๊ฐ!!!!!!!!!!!!!!!!!!!!!!

 

 

 

๐Ÿฅ• <% ์ด์šฉํ•ด ๋‹ค๋ฅธ ํŽ˜์ด์ง€์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ html์— ๋„์›Œ์คŒ

1. <% %> : EJS ์—์„œ ์†Œ์Šค๋‚ด ์—์„œ ์‹คํ–‰๋˜์ง€๋งŒ ๋ณด์ด์ง€๋Š” ์•Š๋Š” ํƒœ๊ทธ
2. <%- %> : <% %> ํƒœ๊ทธ์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ HTML ์ฝ”๋“œ๋ฅผ ๋‚ ๊ฒƒ(Raw)๋กœ ๋ณด์ž„
3. <%= %> : ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ํฌํ•จ, ๋‹ค๋ฅธ ํŽ˜์ด์ง€์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค„ ๋•Œ

 

๐Ÿง views/index.ejs

 <div class="container">
    <div class="row my-4">
      <div class="col-lg-12">
        <!-- router ์— ์žˆ๋Š”  locals.message ๊ฐ€์ ธ์˜ด -->
        <!-- 
        1. <% %> : EJS ์—์„œ ์†Œ์Šค๋‚ด ์—์„œ ์‹คํ–‰๋˜์ง€๋งŒ ๋ณด์ด์ง€๋Š” ์•Š๋Š” ํƒœ๊ทธ
        2. <%- %> : <% %> ํƒœ๊ทธ์™€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ HTML ์ฝ”๋“œ๋ฅผ ๋‚ ๊ฒƒ(Raw)๋กœ ๋ณด์ž„
        3. <%= %> : ์ด์Šค์ผ€์ดํ”„ ๋ฌธ์ž๋ฅผ ํฌํ•จ, ๋‹ค๋ฅธ ํŽ˜์ด์ง€์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค„ ๋•Œ
       -->
        <% if (message) { %>
          <div class="alert alert-dismissible fade show alert-<%= message.type %>" role="alert">
            <button class="btn-close" type="button" data-bs-dismiss="alert" aria-label="Close"></button>
            <strong>
              <%= message.message %>
            </strong>
          </div>
          <% } %>
            <div class="table-responsive">
              <!-- ํ™”๋ฉด ํฌ๊ธฐ์— ๋”ฐ๋ผ ํšจ๊ณผ์ ์œผ๋กœ ์ฝ˜ํ…์ธ ๊ฐ€ ํ‘œ์‹œ๋˜๋„๋ก ํ…Œ์ด๋ธ”์„ ์กฐ์ • -->
              <table class="table table-striped">
                <!-- ํ…Œ์ด๋ธ”์˜ ์—ด์˜ ๋จธ๋ฆฌ๊ธ€์ธ ํ–‰๋“ค์˜ ์ง‘ํ•ฉ -->
                <thead class="table table-striped text-center">
                  <tr class="table-dark">
                    <th>ID</th>
                    <th>Image</th>
                    <th>Name</th>
                    <th>E-mail</th>
                    <th>Phone</th>
                    <th>Action</th>
                  </tr>
                </thead>

                <tbody>
                  <tr>
                    <td>1</td>
                    <td><img src="" alt=""></td>
                    <td>gani</td>
                    <td>gani@test.com</td>
                    <td>010-1234-1234</td>
                    <td>
                      <a href="" class="text-success"><i class="fas fa-edit fa-lg mx-1"></i></a>
                      <a href="" class="text-danger"><i class="fas fa-trash fa-lg mx-1"></i></a>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
      </div>
    </div>
  </div>

  <%- include("layout/footer") %>

user added successful ์ด๋ผ๊ตฌ ๋ฐฐ๋„ˆ๋„ ๋„ฃ์–ด์คŒ!!!!

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