๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[CRUD์ ์ด์ฉํ File upload Web] (7) router์ ์ด๋ฏธ์ง ์ ๋ก๋ (multer) ์ํค๊ณ ๋ฉ์ธ ํ์ผ๋ก redirect ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
[CRUD์ ์ด์ฉํ File upload Web] (7) router์ ์ด๋ฏธ์ง ์ ๋ก๋ (multer) ์ํค๊ณ ๋ฉ์ธ ํ์ผ๋ก redirect
์ง์ง์ํ์นด 2023. 3. 10. 01:46728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ 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") %>
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments