Young's Today
2022-11-30. TIL 본문
** 공부 내용
1. API 설계
(1) app.js
const express = require('express');
const app = express();
const port = 3000;
const commentsRouter = require("./routes/comment.js");
const postsRouter = require("./routes/posts.js");
const connect = require("./schemas");
connect();
app.use(express.json());
app.use("/api", [commentsRouter, postsRouter]);
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(port, '포트로 서버가 열렸어요!');
});
(2) schemas - index.js
const mongoose = require("mongoose");
const connect = () => {
mongoose
.connect("mongodb://127.0.0.1:27017/homework")
// mongoose만의 localhost 에러
.catch(err => console.log(err));
};
mongoose.connection.on("error", err => {
console.error("몽고디비 연결 에러", err);
});
module.exports = connect;
(3) schemas - post.js
const mongoose = require("mongoose");
const postsSchema = new mongoose.Schema({
postId: {
type: Number,
required: true,
unique: true
},
title: {
type: String,
required: true
},
name: {
type: String,
required: true,
},
postsDate: {
type: Number,
required: true
},
password: {
type: String,
required: true
},
contents: {
type: String,
required: true
}
});
module.exports = mongoose.model("posts", postsSchema);
const express = require("express");
const router = express.Router();
const posts = require("../schemas/post.js");
// [게시글 작성 API]
const Posts = require("../schemas/post.js");
router.post("/posts", async (req, res) => {
// const postsDate = new Date(postsDate);
const { postId, title, name, postsDate, password, contents } = req.body;
const posts = await Posts.find({ postId });
if (posts.length) {
return res.status(400).json({
"errorMessage": "데이터 형식이 올바르지 않습니다."});
};
const createdPosts = await Posts.create({ postId, title, name, postsDate, password, contents });
res.json({"message":"게시글을 작성하였습니다."});
});
///// 날짜순서 내림차순 정렬 /////
// [전체 게시글 목록 조회 API]
router.get("/posts", async (req, res) => {
const list = await posts.find();
res.json({ list });
});
// [게시글 상세 조회 API]
router.get("/posts/:postId", async (req, res) => {
const {postId} = req.params;
const detail = await posts.findOne({postId:postId});
if (typeof detail === 'undefined' || detail === null) {
return res.status(400).json({
"errorMessage": "데이터가 없습니다."});
};
const detailPost = detail
res.json({detailPost});
});
// [게시글 수정 API]
router.put("/posts/:postId", async(req,res) => {
const {postId} = req.params;
const password = req.body.password;
const contents = req.body.contents;
const findPosts = await posts.findOne({postId});
if (password !== findPosts.password) {
return res.status(400).json({
"errorMessage": "게시글 수정에 실패하였습니다."});
};
const modifyPosts = await posts.updateOne({ postId: Number(postId) }, { $set: req.body });
res.status(200).json({"message":"게시글을 수정하였습니다."});
});
// [게시글 삭제 API]
router.delete("/posts/:postId", async(req,res) => {
const {postId} = req.params;
const password = req.body.password;
const findPosts = await posts.findOne({postId});
if (typeof findPosts === 'undefined' || findPosts === null) {
return res.status(400).json({"errorMessage":"데이터 형식이 올바르지 않습니다."});
};
if (password === findPosts.password) {
await posts.deleteOne({postId});
return res.status(200).json({"message":"게시글을 삭제하였습니다."});
};
});
module.exports = router;
2. AWS (Ubuntu) / MongoDB / NodeJs / Git / 가비아
- 너무 많은 에러를 만나서 어디서부터 정리해야할까 고민 중
** 느낀 점
- API 작성은 아직 미흡한게 많고 과제를 100% 수행하지도 못한 것 같다.
- 다른 사람들과 속도 비교하지말고 나만의 속도를 찾아가자. 대신 롱런할 것.
** 잘한 점
- N/A
** 못한 점
- N/A
'TW I Learnd' 카테고리의 다른 글
2022-12-02. TIL (0) | 2022.12.02 |
---|---|
2022-12-01. TIL (0) | 2022.12.02 |
2022-11-29. TIL (0) | 2022.11.29 |
2022-11-28. TIL (0) | 2022.11.29 |
2022-11-27. WIL (0) | 2022.11.27 |