|
|
|
@ -8,29 +8,63 @@ const port: number = 8000; |
|
|
|
|
|
|
|
|
|
app.use(cors()); |
|
|
|
|
|
|
|
|
|
// 전체 적용
|
|
|
|
|
// * Logging Middleware
|
|
|
|
|
app.use((req, res, next) => { |
|
|
|
|
console.log(req.rawHeaders[1]); |
|
|
|
|
next(); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// 특정 REST 엔드포인트 적용
|
|
|
|
|
app.get("/cats/blue", (req, res) => { |
|
|
|
|
console.log("This is /cats/blue GET middleware"); |
|
|
|
|
// * json middleware
|
|
|
|
|
app.use(express.json()); |
|
|
|
|
|
|
|
|
|
// * C
|
|
|
|
|
app.post("/cats", (req, res) => { |
|
|
|
|
try { |
|
|
|
|
const data = req.body; |
|
|
|
|
Cat.push(data); |
|
|
|
|
res.status(200).send({ |
|
|
|
|
success: true, |
|
|
|
|
data: { data }, |
|
|
|
|
}); |
|
|
|
|
} catch (error) {} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
app.get("/", (req: express.Request, res: express.Response) => { |
|
|
|
|
res.send({ cats: Cat }); |
|
|
|
|
// * R
|
|
|
|
|
app.get("/cats", (req, res) => { |
|
|
|
|
try { |
|
|
|
|
const cats = Cat; |
|
|
|
|
res.status(200).send({ |
|
|
|
|
success: true, |
|
|
|
|
data: { cats }, |
|
|
|
|
}); |
|
|
|
|
} catch (error) { |
|
|
|
|
res.status(400).send({ |
|
|
|
|
success: false, |
|
|
|
|
error: error.message, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
app.get("/cats/blue", (req, res) => { |
|
|
|
|
res.send({ blue: Cat[0] }); |
|
|
|
|
app.get("/cats/:id", (req, res) => { |
|
|
|
|
try { |
|
|
|
|
const params = req.params; |
|
|
|
|
const cats = Cat.find((cat) => { |
|
|
|
|
return cat.id === params.id; |
|
|
|
|
}); |
|
|
|
|
if (!cats) throw new Error("no matched data"); |
|
|
|
|
res.status(200).send({ |
|
|
|
|
success: true, |
|
|
|
|
data: { cats }, |
|
|
|
|
}); |
|
|
|
|
} catch (error) { |
|
|
|
|
res.status(400).send({ |
|
|
|
|
success: false, |
|
|
|
|
error: error.message, |
|
|
|
|
}); |
|
|
|
|
app.get("/cats/som", (req, res) => { |
|
|
|
|
res.send({ som: Cat[1] }); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// not found
|
|
|
|
|
// * 404 middleware
|
|
|
|
|
app.use((req, res, next) => { |
|
|
|
|
console.log(req.rawHeaders[1]); |
|
|
|
|
console.log("This is 404 middleware"); |
|
|
|
|