routes 폴더에 있는 기능부분을 controllers 폴더로 옮겨 한눈에 보기 쉽도록 깔끔하게 코드를 정리 (Refactoring) 하였다.
👇코드 보러가기
https://github.com/Sara-Jo/BeWild/commit/6e66c37664343d048e73e8f38242f9123182dab8
EX)
[routes/campgrounds.js]
router.get("/", catchAsync(async (req, res) => {
const campgrounds = await Campground.find({});
res.render("campgrounds/campgrounds", { campgrounds });
}));
router.post("/", isLoggedIn, validateCampground, catchAsync(async(req, res) => {
const campground = new Campground(req.body.campground);
campground.author = req.user._id; // Store the author DB when a new campground is created
await campground.save();
req.flash("success", "Successfully made a new campground!");
res.redirect(`/campgrounds/${campground._id}`);
}));
[routes/campgrounds.js]
router.route("/")
.get(catchAsync(campgrounds.index))
.post(isLoggedIn, validateCampground, catchAsync(campgrounds.createCampground));
[controllers/campgrounds.js]
module.exports.index = async (req, res) => {
const campgrounds = await Campground.find({});
res.render("campgrounds/campgrounds", { campgrounds });
}
module.exports.renderNewForm = (req, res) => {
res.render("campgrounds/new");
}
// Create
module.exports.createCampground = async(req, res) => {
const campground = new Campground(req.body.campground);
campground.author = req.user._id; // Store the author DB when a new campground is created
await campground.save();
req.flash("success", "Successfully made a new campground!");
res.redirect(`/campgrounds/${campground._id}`);
}