์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- ํจ์คํธ์บ ํผ์ค
- MVVM
- BFS
- TCA
- ํ๋ก๊ทธ๋๋จธ์ค
- SnapKit
- BOJ
- Lv2
- arkit
- swift
- XCTest
- visionOS
- reactorkit
- Flutter
- Xcode
- RxSwift
- node.js
- realm
- Swfit
- tableView
- rxcocoa
- combine
- Kuring
- CollectionView
- raywenderlich
- designpattern
- SwiftUI
- ๋ฐฑ์ค
- ios
- UIKit
- Today
- Total
lgvv98
[Node.js] ๋ชฝ๊ณ ๋๋น ๋ณธ๋ฌธ
๋ชฝ๊ณ ๋๋น
์ด ์ฅ์์๋ ๋ชฝ๊ณ ๋๋น๋ฅผ ์์๋๋ค๋ฉด ๋์ฑ ๋ ๋ค์ํ ํ๋ก๊ทธ๋จ์ ๋ง๋ค ์ ์๋ค.
๋ชฝ๊ณ ๋๋น์ ํน์ง ์ค ํ๋๋ ์์ค ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ค. ๋ฐ๋ผ์ ๋ชฝ๊ณ ๋๋น ์ฌ์ฉํ๋ฉด ์์ค๋ง์ผ๋ก ์น ์ ํ๋ฆฌ์ผ์ด์ ๋ง๋ค ์ ์์.
# NoSQL vs SQL
MySQL์ SQL์ ์ฌ์ฉํ๋ ๋ํ์ ์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ค. ๋ฐ๋ฉด์ SQL ์ฌ์ฉํ์ง ์๋ NoSQL(Not Only SQL)์ด๋ผ๊ณ ๋ถ๋ฅด๋ ๋ฐ๋ฒ ๋ ์๋ค.
์ฌ๋ฌ ์ธก๋ฉด์์ ๋ค๋ฅด์ง๋ง ๋ช๊ฐ์ง๋ง ์์๋ณด์.
NoSQL์๋ ๊ณ ์ ๋ ํ ์ด๋ธ ์์. ํ ์ด๋ธ์ ์ฌ์ํ๋ ์ปฌ๋ ์ ์ด๋ ๊ฐ๋ ์ ์์ผ๋ ์นผ๋ผ์ ๋ฐ๋ก ์ ์ํ์ง ใ ๋ณ์
MySQL์ users ํ ์ด๋ธ์ ๋ง๋ค ๋ name, age, married ๋ฑ์ ์นผ๋ผ๊ณผ ์๋ฃํ ์ต์ ๋ฑ์ ์ ์ํ์ง๋ง ๋ชฝ๊ณ ๋๋น๋ ๊ทธ๋ฅ users ์ปฌ๋ ์ ์ ๋ง๋ค๊ณ ๋. ์ด๋ ํ ๋ฐ์ดํฐ๋ ๋ค์ด๊ฐ ์ ์์.
๋ชฝ๊ณ ๋๋น๋ JOIN์๋ค. ๊ทธ๋ผ์๋ ์ฐ๋ ์ด์ ๋ ํ์ฅ์ฑ๊ณผ ๊ฐ์ฉ์ฑ.
ํญ๊ณต์ฌ ์์ฝ ํ๋ก๊ทธ๋จ ๋ง๋ ๋ค๋ฉด ์์ฝ์ ๋ณด๋ MySQL ์ฑํ ๊ฐ์ ๋ถ๋ถ์ NoSQL
# ๋ชฝ๊ณ ๋๋น์ค์น
brew tap mongodb/brew
brew install mongodb-community
brew install mongosh
๋งฅ์ ๊ฒฝ์ฐ /usr/local/var/mongodb
๋ชฝ๊ณ ๋๋น ์คํ
brew services start mongodb-community
์ด์ ๋ชฝ๊ณ ๋๋น ํ๋กฌํํธ์ ์ ์ํ์. ์ฝ์์ mongosh ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํด๋ณด์
mongosh
test>
ํน์ ์ ์์ด ๋์ง ์๋๋ค๋ฉด brew services restart mongodb-community๋ก ์๋น์ค๋ฅผ ์ฌ์์ํ ๋ค ๋ค์ mongodb๋ก ์ ์ํฉ๋๋ค.
์ง๊ธ์ ๋๊ตฌ๋ ์ ์ํ ์ ์์ผ๋ฏ๋ก ๊ด๋ฆฌ์ ๊ณ์ ์ ์ถ๊ฐํฉ๋๋ค.
use admin
switched to db admin
admin> db.createUser({ user: '์ด๋ฆ', pwd: '๋น๋ฐ๋ฒํธ', roles: ['root']})
{ ok: 1 }
db.createUser๋ก ๊ณ์ ์ ์์ฑํ ์ ์๋ค.
roles๋ก๋ ๋ชจ๋ ๊ถํ์ ์ฌ์ฉํ ์ ์๋ root๋ฅผ ๋ถ์ฌํ๋ค.
๋ชฝ๊ณ ๋๋น ์ข ๋ฃํ๊ณ
brew services stop mongodb-community
์ํ๋ฆฌํฐ ์ค์
์ธํ
vim /usr/local/etc/mongod.conf
์ค๋ฆฌ์ฝ
vim /opt/homebrew/etc/mongod.conf
ํ ํ
...
sercurity:
authorization: enabled
์
๋ ฅํ ์ ์ฅ(wq)
๋ค์ ์คํstartํ๊ณ
monogsh admin -u ์ด๋ฆ -p ๋น๋ฐ๋ฒํธ
์ ๋ ฅํ๋ฉด
admin> ๋ ๊ฑฐ์.
# ์ปดํผ์ค ์ค์น
brew install --cask mongodb-compass-community
https://www.mongodb.com/try/download/compass
์ค์นํ๋ฉด ๋ฐ์นํจ๋์ ๋ํ๋๋๋ฐ ์๋ ๋๋ ํด๋น ์ฌ์ดํธ ์ง์ ๋ค์ด๊ฐ์ ํ๋ฉด ๋ผ
Advanced Connection Options -> Username/Password๋ก ๋ฐ๊พธ๊ณ
Username์ด๋ password ๋น ์นธ์ผ๋ก ๋ ์ฑ ์ ๋ ฅํ๋ฉด ์ธ์ด๋ธ
๋ค์ ์ฝ์์์
brew services start mongodb-community
mongosh
๋ฐ์ดํฐ ๋ฒ ์ด์ค ๋ง๋๋ ๋ช ๋ น์ด๋
use nodejs
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ชฉ๋ก์ ํ์ธํ๋ ๋ช ๋ น์ด๋
show dbs
ํ์ฌ ์ฌ์ฉ์ค์ธ ๋ฐ๋ฒ ํ์ธํ๋ ๋ช ๋ น์ด๋
db
๋ฐ๋ฒ ๋ชฉ๋ก์ ์์ง๋ง nodejs ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฌ์ฉํ๊ณ ์๋ค๋ ๊ฒ ์ ์ ์์.
์ปฌ๋ ์ ๋ง๋ค๋ ค๋ฉด
nodejs> db.createCollection('users')
nodejs> db.createCollection('comments')
# CRUD ์์ ํ๊ธฐ
๋ชฝ๊ณ ๋๋น์์ CRUD์์ ํด๋ณด์.
# Create(์์ฑ)
Date๋ ์ ๊ทํํ์๊ณผ ๊ฐ์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ฅผ ์๋ฃํ์ผ๋ก ์ฌ์ฉํ ์ ์์. Binary Data, OjectId, Int, Long, Demical, Timestamp, JavaScript ๋ฑ ์ถ๊ฐ์ ์ธ ์๋ฃํ ์กด์ฌ. ๊ทธ๋ฌ๋ ObjectId, Binary Data, Timestamp ์ธ์๋ ์ ์ฌ์ฉํ์ง ์๊ณ Undefined์ Symbol์ ๋ชฝ๊ณ ๋๋น์์ ์๋ฃํ์ผ๋ก ์ฌ์ฉํ์ง ์์.
ObjectId๋ MySQL์์ ํค ๊ฐ๊ณผ ๊ฐ๋ค.
์ ๋ก์ถ๊ฐ
db.users.insertOne({ name: 'zero', age: 24, married: false, comment: '์๋
ํ์ธ์. ๋ชฝ๊ณ ๋๋น ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด์', createAt: new Date() })
๋ค๋ก์ถ๊ฐ
db.users.insertOne({ name: 'nero', age: 32, married: true, comment: '์๋
ํ์ธ์. zero์น๊ตฌ nero์
๋๋ค', createAt: new Date() }
๋๊ธ ์ถ๊ฐ
db.comments.insertOne({ commenter: ObjectId('64d477ea74ecde1716e7628e'), comment: '์๋
ํ์ธ์ zero์
๋๋ค', createAt: new Date()})
# Read(์กฐํ)
find({});๋ ์ปฌ๋ ์ ๋ด์ ๋ชจ๋ ๋คํ๋จผํธ๋ฅผ ์กฐํํ๋ผ๋ ์๋ฏธ
db.users.find({});
// ๊ฒฐ๊ณผ
[
{
_id: ObjectId("64d477ea74ecde1716e7628e"),
name: 'zero',
age: 24,
married: false,
comment: '์๋
ํ์ธ์. ๋ชฝ๊ณ ๋๋น ์ฌ์ฉ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณด์',
createAt: ISODate("2023-08-10T09:38:50.539Z")
},
{
_id: ObjectId("64d4783974ecde1716e7628f"),
name: 'nero',
age: 32,
married: true,
comment: '์๋
ํ์ธ์. zero์น๊ตฌ nero์
๋๋ค',
createAt: ISODate("2023-08-10T09:40:09.292Z")
}
]
ํน์ ํ๋๋ง ์กฐํํ๊ณ ์ถ๋ค๋ฉด
find๋ฉ์๋์ ๋๋ฒ์งธ ์ธ์๋ก ์กฐํํ ํ๋๋ฅผ ๋ฃ๋๋ค 1๋๋ true๋ก ํ์ํ ํ๋๋ง ๊ฐ์ ธ์จ๋ค.
_id๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ฒ ๋์์ด์ 0๋๋ false ์ ๋ ฅํด ๊ฐ์ ธ์ค์ง ์๊ฒ ํํํ๋ค.
db.users.find({}, {_id: 0, name: 1, married: 1});
[ { name: 'zero', married: false }, { name: 'nero', married: true } ]
์กฐํ์ ์กฐ๊ฑด์ ์ข ๋ ์ค๋ณด์
$gt๋ ์ด๊ณผ์ด๋ ๋ป
gt ์ด๊ณผ
gte ์ด์
lt ๋ฏธ๋ง
lte ์ดํ
ne ๊ฐ์ง ์์
or ๋๋
in ๋ฐฐ์ด ์ ์์ ์คํ๋
db.users.find({age: { $gt: 30 }, married: true} ,{_id: 0, name: 1, married: 1});
// ๊ฒฐ๊ณผ
[ { name: 'nero', married: true } ]
or์ ์ฌ์ฉํ๋ ์์๋ค.
db.users.find({ $or: [{ age: { $gt: 30 } }, { married: false }] }, { _id: 0, name: 1, age: 1 });
// ๊ฒฐ๊ณผ
[ { name: 'zero', age: 24 }, { name: 'nero', age: 32 } ]
์ด๋ฒ์๋ ์ ๋ ฌ
db.users.find({}, { _id: 0, name: 1, age: 1 }).sort({ age: -1 })
// ๊ฒฐ๊ณผ
[ { name: 'nero', age: 32 }, { name: 'zero', age: 24 } ]
๊ฐ์์ ํ
db.users.find({}, { _id: 0, name: 1, age: 1 }).sort({ age: -1 }).limit(1)
// ๊ฒฐ๊ณผ
[ { name: 'nero', age: 32 } ]
๋คํ๋จผํธ ๊ฐ์ ๋ช๊ฐ ๊ฑด๋๋ฐ๊ธฐ๋ ๊ฐ๋ฅ
db.users.find({}, { _id: 0, name: 1, age: 1 }).sort({ age: -1 }).limit(1).skip(1)
// ๊ฒฐ๊ณผ
[ { name: 'zero', age: 24 } ]
# Update(์์ )
db.users.updateOne( { name: 'nero' }, { $set: { comment: '์๋
ํ์ธ์. ์ด ํ๋๋ฅผ ๋ฐ๊ฟ๋ด
์๋ค' } });
// ๊ฒฐ๊ณผ
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
$set์ด๋ผ๋ ์ฐ์ฐ์๋ ์ด๋คํ๋๋ฅผ ์์ ํ ์ง ์ ํ๋ ์ฐ์ฐ์ ์ด๊ฑฐ ์ฌ์ฉํ์ง ์๊ณ ์ผ๋ฐ ๊ฐ์ฒด๋ฅผ ๋ฃ๋๋ค๋ฉด ๋คํ๋จผํธ ํต์จฐ๋ก ๋๋ฒ์จฐ ์ธ์๋ก ์ฃผ์ด์ง ๊ฐ์ฒด๋ก ์์ ๋๋ค.
set
# Delete(์ญ์ )
db.users.deleteOne({ name: 'nero' })
์ญ์ ํ ๋คํ๋จผํธ์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ๊ฐ์ฒด ์ ๊ณตํ๋ฉด ๋๋ค.
# ๋ชฝ๊ตฌ์ค ์ฌ์ฉํ๊ธฐ
MySQL์๋ ์ํ๋ผ์ด์ ? ๊ทธ๋ผ ๋ชฝ๊ณ ๋๋น๋ ๋ชฝ๊ตฌ์ค
๋ชฝ๊ตฌ์ค๋ ์ํ๋ผ์ด์ฆ์ ๋ฌ๋ฆฌ ODM์ด๋ผ๊ณ ๋ถ๋ฆผ. ๋ชฝ๊ณ ๋๋น๋ ๋ฆด๋ ์ด์ ์ด ์๋๋ผ ๋คํ๋จผํธ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ORM์ด ์๋๋ผ ODM์.
๋จผ์ ์คํค๋ง๋ผ๋๊ฒ ์๊น ๋ชฝ๊ณ ๋๋น๋ ํ ์ด๋ธ์ด ์์ด์ ์์ ๋กญ๊ฒ ํ ์ด๋ธ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ ์ ์์ง๋ง ์์ ๋ก์์ด ๋ถํธํจ์ ์ด๋ํ๊ธฐ๋ ํจ.
JOIN๊ธฐ๋ฅ์ populate๋ผ๋ ๋ฉ์๋๋ฅผ ์ด๋ ์ ๋ ๋ณด์
package.json์์ฑ
{
"name": "learn-sequelize",
"version": "0.0.1",
"description": "์ํ๋ผ์ด์ฆ๋ฅผ ๋ฐฐ์ฐ์",
"main": "app.js",
"scripts": {
"start": "nodemon app"
},
"author": "ZeroCho",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"morgan": "^1.10.0",
"mysql2": "^2.1.0",
"nunjucks": "^3.2.2",
"sequelize": "^6.3.4",
"sequelize-cli": "^6.2.0"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}
npm i express morgan nunjucks mongoose
npm i -D nodemon
๋ชฝ๊ตฌ์ค์ ํ์ํ ํจํค์ง ์ค์น
๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐํ๊ธฐ
๋ชฝ๊ณ ๋๋น๋ ์ฃผ์๋ฅผ ์ฌ์ฉํด ์ฐ๊ฒฐ
mongodb://[username:password@]host[:port][/[database][?options]]
mongodb://์ด๋ฆ:๋น๋ฐ๋ฒํธ@localhost:27017/admin
์ด ํํ์ ๊ฐ๋ค. [ ] ๋ถ๋ถ์ ์์ด๋ ๋๊ณ ์์ด๋ ๊ทธ๋ง!
schemas ํด๋๋ฅผ ๋ง๋ ๋ค์์ index.jsํ์ผ์ ์์ฑ
const mongoose = require('mongoose');
const connect = () => {
if (process.env.NODE_ENV != 'production') {
mongoose.set('debug', true);
}
mongoose.connect('mongodb://localhost:27017'), { // โ
์ด๊ฑฐ ๊ฐ์ค์
dbName: 'nodejs',
useNewUrlParser: true,
}, (error) => {
if error {
console.log('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ', error);
} else {
console.log('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์ฑ๊ณต');
}
});
};
mongoose.connection.on('error', (error) => {
console.error('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ ์๋ฌ', error);
});
mongoose.connection.on('error', (error) => {
console.error('๋ชฝ๊ณ ๋๋น ์ฐ๊ฒฐ์ด ๋๊ฒผ์ตใด๋ใ
. ์ฐ๊ฒฐ์ ์ฌ์๋ ํฉ๋๋ค.', error);
});
module.exports = connect;
๊ฐ๋ฐ ํ๊ฒฝ์ผ ๋๋ง ์ฝ์์ ํตํด ๋ชฝ๊ตฌ์ค๊ฐ ์์ฑํ๋ ์ฟผ๋ฆฌ ๋ด์ฉ์ ํ์ธํ ์ ์๊ฒ ํ๋ ์ฝ๋
์คํค๋ง๋ฅผ ์ ์ํด๋ณด์
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
name: {
type: String,
required: true,
unique: true,
},
age: {
type: Number,
required: true,
},
married: {
type: Boolean,
required: true,
},
comment: String,
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('User', userSchema);
const mongoose = require('mongoose');
const { Schema } = mongoose;
const userSchema = new Schema({
name: {
type: String,
required: true,
unique: true,
},
age: {
type: Number,
required: true,
},
married: {
type: Boolean,
required: true,
},
comment: String,
createdAt: {
type: Date,
default: Date.now,
},
});
module.exports = mongoose.model('User', userSchema);
์ปฌ๋ ์ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ
mongoose.model('User', userSchema, 'user_table');
User์ผ ๊ฒฝ์ฐ user๋ก ๊ฐ์ ๋ก ์์ฑ๋๋๋ฐ ์์ฒ๋ผ ๋ฐ๊พธ๊ธฐ ๊ฐ๋ฅ
# ์ฟผ๋ฆฌ ์ํํ๊ธฐ
// ์ฌ์ฉ์ ์ด๋ฆ ๋๋ ์ ๋ ๋๊ธ ๋ก๋ฉ
document.querySelectorAll('#user-list tr').forEach((el) => {
el.addEventListener('click', function () {
const id = el.querySelector('td').textContent;
getComment(id);
});
});
// ์ฌ์ฉ์ ๋ก๋ฉ
async function getUser() {
try {
const res = await axios.get('/users');
const users = res.data;
console.log(users);
const tbody = document.querySelector('#user-list tbody');
tbody.innerHTML = '';
users.map(function (user) {
const row = document.createElement('tr');
row.addEventListener('click', () => {
getComment(user._id);
});
// ๋ก์ฐ ์
์ถ๊ฐ
let td = document.createElement('td');
td.textContent = user._id;
row.appendChild(td);
td = document.createElement('td');
td.textContent = user.name;
row.appendChild(td);
td = document.createElement('td');
td.textContent = user.age;
row.appendChild(td);
td = document.createElement('td');
td.textContent = user.married ? '๊ธฐํผ' : '๋ฏธํผ';
row.appendChild(td);
tbody.appendChild(row);
});
} catch (err) {
console.error(err);
}
}
// ๋๊ธ ๋ก๋ฉ
async function getComment(id) {
try {
const res = await axios.get(`/users/${id}/comments`);
const comments = res.data;
const tbody = document.querySelector('#comment-list tbody');
tbody.innerHTML = '';
comments.map(function (comment) {
// ๋ก์ฐ ์
์ถ๊ฐ
const row = document.createElement('tr');
let td = document.createElement('td');
td.textContent = comment._id;
row.appendChild(td);
td = document.createElement('td');
td.textContent = comment.commenter.name;
row.appendChild(td);
td = document.createElement('td');
td.textContent = comment.comment;
row.appendChild(td);
const edit = document.createElement('button');
edit.textContent = '์์ ';
edit.addEventListener('click', async () => { // ์์ ํด๋ฆญ ์
const newComment = prompt('๋ฐ๊ฟ ๋ด์ฉ์ ์
๋ ฅํ์ธ์');
if (!newComment) {
return alert('๋ด์ฉ์ ๋ฐ๋์ ์
๋ ฅํ์
์ผ ํฉ๋๋ค');
}
try {
await axios.patch(`/comments/${comment._id}`, { comment: newComment });
getComment(id);
} catch (err) {
console.error(err);
}
});
const remove = document.createElement('button');
remove.textContent = '์ญ์ ';
remove.addEventListener('click', async () => { // ์ญ์ ํด๋ฆญ ์
try {
await axios.delete(`/comments/${comment._id}`);
getComment(id);
} catch (err) {
console.error(err);
}
});
// ๋ฒํผ ์ถ๊ฐ
td = document.createElement('td');
td.appendChild(edit);
row.appendChild(td);
td = document.createElement('td');
td.appendChild(remove);
row.appendChild(td);
tbody.appendChild(row);
});
} catch (err) {
console.error(err);
}
}
// ์ฌ์ฉ์ ๋ฑ๋ก ์
document.getElementById('user-form').addEventListener('submit', async (e) => {
e.preventDefault();
const name = e.target.username.value;
const age = e.target.age.value;
const married = e.target.married.checked;
if (!name) {
return alert('์ด๋ฆ์ ์
๋ ฅํ์ธ์');
}
if (!age) {
return alert('๋์ด๋ฅผ ์
๋ ฅํ์ธ์');
}
try {
await axios.post('/users', { name, age, married });
getUser();
} catch (err) {
console.error(err);
}
e.target.username.value = '';
e.target.age.value = '';
e.target.married.checked = false;
});
// ๋๊ธ ๋ฑ๋ก ์
document.getElementById('comment-form').addEventListener('submit', async (e) => {
e.preventDefault();
const id = e.target.userid.value;
const comment = e.target.comment.value;
if (!id) {
return alert('์์ด๋๋ฅผ ์
๋ ฅํ์ธ์');
}
if (!comment) {
return alert('๋๊ธ์ ์
๋ ฅํ์ธ์');
}
try {
await axios.post('/comments', { id, comment });
getComment(id);
} catch (err) {
console.error(err);
}
e.target.userid.value = '';
e.target.comment.value = '';
});
comments.js๋ฅผ ๋ณด์
const express = require('express');
const Comment = require('../schemas/comment');
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);
const result = await Comment.populate(comment, { path: 'commenter' });
res.status(201).json(result);
} catch (err) {
console.error(err);
next(err);
}
});
router.route('/:id')
.patch(async (req, res, next) => {
try {
const result = await Comment.update({
_id: req.params.id,
}, {
comment: req.body.comment,
});
res.json(result);
} catch (err) {
console.error(err);
next(err);
}
})
.delete(async (req, res, next) => {
try {
const result = await Comment.remove({ _id: req.params.id });
res.json(result);
} catch (err) {
console.error(err);
next(err);
}
});
module.exports = router;
๋๊ธ์ ๊ด๋ จ๋ CRUD ์์ ์ ํ๋ ๋ผ์ฐํฐ์ ๋๋ค.
'๐ฐ๏ธ Node.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node.js] #7 MySQL (0) | 2023.07.23 |
---|---|
[Node.js] #6 ์ต์คํ๋ ์ค ์น ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.07.23 |
[Node.js] #5 ํจํค์ง ๋งค๋์ (0) | 2023.07.23 |
[Node.js] #4 http ๋ชจ๋๋ก ์๋ฒ ๋ง๋ค๊ธฐ (0) | 2023.07.22 |
[Node.js] #3 ๋ ธ๋ ๊ธฐ๋ฅ ์์๋ณด๊ธฐ (0) | 2023.07.22 |