-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (82 loc) · 2.69 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID; // we will use this later
MongoClient.connect('mongodb://admin1:[email protected]:59089/crud_using_node', (err, database) => {
var dbase = database.db("crud_using_node");
if (err) return console.log(err)
app.listen(3000, () => {
console.log('app working on 3000')
})
// Creating an Entry
app.post('/name/add', (req, res, next) => {
var name = {
first_name: req.body.first_name,
last_name: req.body.last_name
};
dbase.collection("name").save(name, (err, result) => {
if (err) {
console.log(err);
}
res.send('name added successfully');
});
});
// To test this application for Adding an Entry:
// open Postman
// tick x - www - form - urlencoded
// make a post request to http://localhost:3000/name/add with the key as first_name and last_name
// Reading All Entries
app.get('/name', (req, res) => {
dbase.collection('name').find().toArray((err, results) => {
res.send(results)
});
});
// Reading by ID
app.get('/name/:id', (req, res, next) => {
if (err) {
throw err;
}
let id = ObjectID(req.params.id);
dbase.collection('name').find(id).toArray((err, result) => {
if (err) {
throw err;
}
res.send(result);
});
});
// Updating by ID
app.put('/name/update/:id', (req, res, next) => {
let id = {
_id: ObjectID(req.params.id)
};
dbase.collection("name").update({ _id: id }, { $set: { 'first_name': req.body.first_name, 'last_name': req.body.last_name } }, (err, result) => {
if (err) {
throw err;
}
res.send('user updated sucessfully');
});
});
// Deleting by ID
app.delete('/name/delete/:id', (req, res, next) => {
let id = ObjectID(req.params.id);
dbase.collection('name').deleteOne(id, (err, result) => {
if (err) {
throw err;
}
res.send('user deleted');
});
});
})
// app.listen(3000, function () {
// console.log('listening on 3000');
// });
// app.get('/', function (req, res) {
// res.send("Yep it's working");
// });
// app.get('/world', (req, res) => {
// res.send('Hello world!!');
// });