section 20
req.params
uuid
app.get("/restaurants/:id", (req, res) => {
const id = req.params.id;
res.render("restaurant-detail", { id });
});
/ 를 붙여야 절대경로(도메인 뒤에 붙는다)
400 유저프론트 문제
500 서버문제. 예를 들어 많은 유저가 동시에, 파일이 없거나.
catch all
error
맨 마지막줄에
app.use((req, res) => {
res.status(404).render("404");
});
app.use((error, req, res, next) => {
res.status(500).render("500");
});
app.listen(3000);
app.js가 너무 길고 복잡해졌다. 어떻게 간단하게 바꿀수 있을까
split common functions into util folder
const { getStoredRestaurants } = require('./util/restaurant-data');
const filePath = path.join(__dirname, "..", "data", "restaurants.json");
function getStoredRestaurants() {
///
}
module.exports = {
getStoredRestaurants: getStoredRestaurants,
storeRestaurants: storeRestaurants,
};
don't make new app by express.
use express.Router()
as app
routes/default.js
express.Router()
const express = require('express');
const router = express.Router();
// app.get => router.get
router.get("/", (req, res) => {
res.render("index");
});
module.exports = router;
use middleware
every request starts with / go into defaultRoutes and find (not exact path)
app.use("/", defaultRoutes);
app.use("/", restaurantRoutes);
sort((a, b) => if(a > b))
a > b return 1 // flip
else return -1
?...
라우팅을 결정할때 쿼리스트링은 ignored