-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
64 lines (57 loc) · 1.77 KB
/
server.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 app = express()
const port = 3000;
const mongo = require('mongodb');
const bodyParser = require('body-parser');
const MongoClient = mongo.MongoClient;
let db;
const mongoUrl = 'mongodb://127.0.0.1:27017';
const db_name = 'test';
const col_name = 'products';
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json());
app.get('/', (req, res) => res.send('Node js Crud Application'));
app.get('/products', (req, res) =>{
db.collection(col_name).find().toArray((err, result )=>{
if(err) throw err;
res.status(200).send(result)
})
})
app.post('/addproducts', (req, res) => {
db.collection(col_name)
.insertOne(req.body), (err, result ) =>{
if(err) throw err;
res.status(200).send('Data Inserted')
res.end();
}
})
app.put('/updateproducts', (req, res) => {
db.collection(col_name)
.findOneAndUpdate(
{'id' : req.body.id},{
$set : {
name : req.body.name,
father : req.body.father,
Class : req.body.Class
}
},(err, result) => {
if(err) throw err;
res.status(200).send('Data Updated')
res.end()
})
})
app.delete('/deleteuser', (req, res) => {
db.collection(col_name).findOneAndDelete(
{"id" : req.body.id}
,(err, result ) => {
if(err) throw err;
res.status(200).send('Data Deleted SuccessFully')
})
})
MongoClient.connect(mongoUrl, (err, clients) =>{
if(err) throw err;
db = clients.db('classpractice');
app.listen(port, (err) =>{
console.log(`app is running on ${port}`)
})
})