nodemon 실행 명령어
"scripts":{
"start":"nodemon"
-$ npm start app.js
또는
"scripts":{
"start":"nodemon app.js"
-$ npm start
미들웨어 검증
const { authorization } = req.headers; // cookies에서 헤더로 변경
로그인 하라는 에러 메세지 뜸
cl(authType, authToken) -> 쿠키와 비교하여 잘 뜨도록 헤더 수정
console.log(authType !== "Bearer", !authToken); --> false false
미들웨어가 아닌 라우터쪽 에러였음
// Cookie가 존재하지 않을 경우
if (!req.headers) {
// Cookie가 비정상적이거나 만료된 경우
if (!req.headers.authorization) {
cookie-parser 사용
// /routes/index.js
const cookieParser = require("cookie-parser");
router.use(cookieParser());
// 라우트 쪽 다 cookie로 바꿔줌
게시글조회 응답 형태 수정
참고링크
https://velog.io/@ragnarok_code/%ED%94%84%EB%A1%9C%EC%A0%9D%ED%8A%B8-%EC%A0%95%EB%A6%AC-populate
https://mongoosejs.com/docs/populate.html
참고 검색 키워드
mongoose populate options
mongoose populate unnested
// 게시글 목록 조회 api
.populate()를 사용해서 참조 요소 꺼내옴
.then() 과 map함수를 사용해 조회 응답 폼 만듬
// 게시글 상세 조회 API
const _post = {
userId: post.userId._id, // 참조한 요소의 _id값만
nickname: post.userId.nickname, // 참조한 요소의 닉네임
};
res.json({ post: _post });
userid 세가지 요소가 세트처럼 같이 불려오는 오류
user virtual값이 붙는 순서가 요소설정 이후라서 제외하는 조건을 설정해줘도 자꾸 생성됨
버츄얼 속성에서 id 세개를 빼줌
transform(doc, user) {
// ret.id = ret._id;
delete user.id;
delete user._id;
delete user.userId;
},
댓글 작성, 조회 기능 구현
res.locals.user._id를 불러와서 userId로 사용
url에서 postId param으로 받아와서 postId로 사용
userId,postId,comment로 댓글 생성
게시글조회와 비슷한 방식으로 조회 응답 폼 만듬
게시글, 댓글에 updatedAt 추가
schemas에서 먼저 추가하고, routes/~~.js 파일에서 수정 api에
게시글 수정 시 updatedAt 갱신하도록 코드 추가