Page 325 - Nodejs 교과서 개정2판
P. 325
}
});
module.exports = router;
GET /users৬ POST /users ࣗ۽ ਃ ٜযৢ ٸ ۄఠੑפ пп ࢎਊܳ ઑഥೞח ਃҗ ࢎਊܳ
١۾ೞח ਃਸ ܻפ GET /ীࢲب ࢎਊ ؘఠܳ ઑഥ೮݅ GET /usersীࢲח ؘఠܳ +40/ ഋ
धਵ۽ ߈ജೠח Ѫী ରо णפ
GET /users id/comments ۄఠীח findAll ݫࢲ٘ী ২࣌ ୶оغয णפ include ২࣌ীࢲ model
ࣘࢿীח 6TFS ݽ؛ਸ where ࣘࢿীח :id۽ ߉ ই٣ чਸ ֍णפ :idח ۄ ݒѐ߸ࣻ۽ ী
ࢲ ࢸݺ೮णפ req.params.id۽ чਸ оઉৢ ࣻ णפ GET /users/1/commentsۄݶ ࢎਊ idо 1ੋ ؆
Ӗਸ ࠛ۞২פ ઑഥػ ؆Ӗ ёীח include۽ ֍যળ ࢎਊ ࠁب ٜয ਵ۽ ࢿ ܴա ա
١ਸ ઑഥೡ ࣻ णפ
DPNNFOUT KTੑפ
routes/comments.js
const express = require('express');
const { User, Comment } = require('../models');
const router = express.Router();
router.post('/', async (req, res, next) => {
try {
const comment = await Comment.create({
commenter: req.body.id,
comment: req.body.comment,
});
console.log(comment);
res.status(201).json(comment);
} catch (err) {
console.error(err);
next(err);
}
});
router.route('/:id')
.patch(async (req, res, next) => {
try {
const result = await Comment.update({
comment: req.body.comment,
}, {