-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (50 loc) · 1.57 KB
/
app.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
'use strict';
const express = require('express'),
const bodyParser = require('body-parser'),
const mongoose = require('mongoose');
var ObjectId = mongoose.ObjectId;
var User = require('./models/User');
const app = express();
mongoose.connect('mongodb://localhost/nodejscrud', { useNewUrlParser: true });
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/user/insert', (req, res) => {
var myData = new User(req.body);
myData.save()
.then(item => {
res.send("your data is saved successfully");
})
.catch(err => {
res.status(400).send("error in saving data");
});
});
app.put('/user/update/:id', (req, res) => {
var query = { _id: req.params.id };
User.updateOne(query, { $set: { username: req.body.username, title: req.body.title, description: req.body.description } }, (err) => {
if (err) {
console.log(err);
}
res.send('your data is deleted successfully');
})
});
app.get('/user', (req, res) => {
User.find({})
.then((data) => {
res.json(data);
})
.catch((err) => {
res.send("error in getting data" + err);
});
});
app.delete('/user/delete/:id', (req, res) =>{
User.deleteOne({_id: req.params.id}, (err) => {
if(err){
console.log(err);
}
res.send('your data is deleted successfully');
})
});
const server = app.listen(3000, function () {
const port = server.address().port
console.log("app listening at ", port)
});