forked from mehulmpt/node-mongodb-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
64 lines (47 loc) · 1.26 KB
/
script.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express')
const path = require('path')
const app = express()
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const Todo = require('./models/todo')
mongoose.connect('mongodb://localhost/firstmongo')
app.use('/', express.static(path.resolve(__dirname, 'assets')))
app.use(bodyParser.json())
app.post('/api/delete', async (req, res) => {
const { record } = req.body
console.log(record, '/api/delete')
const response = await Todo.deleteOne({ record })
console.log(response, '/api/delete repsonse')
res.json({ status: 'ok' })
})
app.post('/api/modify', async (req, res) => {
const { old: oldTitle, new: newTitle } = req.body
const response = await Todo.updateOne(
{
record: oldTitle
},
{
$set: {
record: newTitle
}
}
)
console.log(response)
res.json({ status: 'ok' })
})
app.get('/api/get', async (req, res) => {
const records = await Todo.find({})
// console.log('Response => ', records)
res.json(records)
})
app.post('/api/create', async (req, res) => {
const record = req.body
console.log(record)
// * CREATE (_C_RUD)
const response = await Todo.create(record)
console.log(response)
res.json({ status: 'ok' })
})
app.listen(13371, '127.0.0.1', () => {
console.log('Server up')
})