준비 사항
- MongoDB 설치, 서버 가동
- MongoDB GUI Tool - Robo 3T 설치
Http 메소드와 Endpoint에 따른 요청을 정리하면 다음과 같다.
HTTP Methods | /articles | /articles/sports |
GET | Fetches all the articles | Fetches the article on sports |
POST | Creates one new article | - |
PUT | - | Updates the article on sports |
PATCH | - | Updates the article on sports |
DELETE | Deletes all the articles | Deletes the article on sports |
1. 기본 프로젝트 세팅
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
자세한 설명은 Express 프로젝트 Boilerplate 글 참조
2. MongoDB에 연결
mongoose.connect("mongodb://localhost:27017/wikiDB", {
useNewUrlParser: true,
useUnifiedTopology: true
}); // allows to use local MongoDB
const articleSchema = new mongoose.Schema({
title: String,
content: String
});
const Article = mongoose.model("Article", articleSchema);
3-1. 전체 articles에 대한 메소드 구현
app.route("/articles")
.get(function(req, res) {
Article.find(function(err, foundArticles) {
if (err) {
res.send(err);
} else {
res.send(foundArticles);
}
});
})
.post(function(req, res) {
console.log(req.body.title);
console.log(req.body.content);
const newArticle = new Article({
title: req.body.title,
content: req.body.content
});
newArticle.save(function(err) {
if (err) {
res.send(err);
} else {
res.send("Successfully added a new article");
}
});
})
.delete(function(req, res) {
Article.deleteMany({}, function(err) {
if (err) {
res.send(err);
} else {
res.send("Successfully deleted data");
}
});
});
3-2. 특정 article에 대한 메소드 구현
app.route("/articles/:articleTitle")
.get(function(req, res) {
Article.find({
title: req.params.articleTitle
}, function(err, foundArticle) {
if (err) {
res.send("No articles matched!");
} else {
res.send(foundArticle);
}
});
})
.put(function(req, res) {
Article.update({
title: req.params.articleTitle
}, {
title: req.body.title,
content: req.body.content
}, {
overwrite: true
},
function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully updated data!");
}
}
);
})
.patch(function(req, res) {
Article.update({
title: req.params.articleTitle
}, {
$set: req.body
}, function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully updated data!");
}
});
})
.delete(function(req, res) {
Article.deleteOne({
title: req.params.articleTitle
}, function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully deleted data!");
}
})
});
코드 전문
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static("public"));
mongoose.connect("mongodb://localhost:27017/wikiDB", {
useNewUrlParser: true,
useUnifiedTopology: true
}); // allows to use local MongoDB
const articleSchema = new mongoose.Schema({
title: String,
content: String
});
const Article = mongoose.model("Article", articleSchema);
//////////////////// Request targeting All Articles ////////////////
app.route("/articles")
.get(function(req, res) {
Article.find(function(err, foundArticles) {
if (err) {
res.send(err);
} else {
res.send(foundArticles);
}
});
})
.post(function(req, res) {
console.log(req.body.title);
console.log(req.body.content);
const newArticle = new Article({
title: req.body.title,
content: req.body.content
});
newArticle.save(function(err) {
if (err) {
res.send(err);
} else {
res.send("Successfully added a new article");
}
});
})
.delete(function(req, res) {
Article.deleteMany({}, function(err) {
if (err) {
res.send(err);
} else {
res.send("Successfully deleted data");
}
});
});
//////////////////// Request targeting specific Articles ////////////////
app.route("/articles/:articleTitle")
.get(function(req, res) {
Article.find({
title: req.params.articleTitle
}, function(err, foundArticle) {
if (err) {
res.send("No articles matched!");
} else {
res.send(foundArticle);
}
});
})
.put(function(req, res) {
Article.update({
title: req.params.articleTitle
}, {
title: req.body.title,
content: req.body.content
}, {
overwrite: true
},
function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully updated data!");
}
}
);
})
.patch(function(req, res) {
Article.update({
title: req.params.articleTitle
}, {
$set: req.body
}, function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully updated data!");
}
});
})
.delete(function(req, res) {
Article.deleteOne({
title: req.params.articleTitle
}, function(err) {
if (err) {
res.send("No articles matched!");
} else {
res.send("Successfully deleted data!");
}
})
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
'web > nodejs' 카테고리의 다른 글
Express 프로젝트 Boilerplate (0) | 2020.04.08 |
---|---|
Mongoose 사용법 -Node에서 mongoDB 연결 + CRUD (0) | 2020.04.03 |
.gitignore for Node (0) | 2020.04.01 |