-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (108 loc) · 3.22 KB
/
index.js
File metadata and controls
159 lines (108 loc) · 3.22 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// grab the packages we need
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const app = express();
// Connection URL
const url = 'mongodb://localhost:27017';
app.use(express.json()); // receive all in jscon format
const port = process.env.PORT || 4000;
const db_name ="Coaches";
const collection_name ="coaches"
let responses = null ;
let lteData = {}
// Create a new collection
function createMyCollection(collection_name,db_name){
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(db_name);
dbo.createCollection(collection_name, function(err, res) {
if (err) throw err;
console.log("Collection created!");
db.close();
console.log(res)
return res
});
});
};
// connect to our mongodb database
let cashedClient = null;
let cashedDb = null
async function connectToDatabase(){
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");
});
const db = client.db(dbName);
return db;
}
// create our route
app.get('/',async(req,res)=>{
res.send('Welcome to Rail Pulse');
})
app.get('/data',async(req,res)=>{
res.send(lteData);
})
app.get('/lte/:id',async(req,res)=>{
// console.log(JSON.stringify(req.query))
lteData = JSON.stringify(req.query)
res.send(JSON.stringify(req.query))
})
app.get('/mc',async(req,res)=>{
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(db_name);
dbo.collection(collection_name).find({}).toArray(function(err, resp) {
if (err) throw err;
res.send({resp})
db.close();
});
});
})
// create
app.post('/mc',async(req,res)=>{
const new_Data = req.body.new_Data
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(db_name);
var myobj = new_Data;
dbo.collection(collection_name).insertOne(myobj, function(err, resp) {
if (err) throw err;
res.send({resp})
db.close();
});
});
})
app.put('/mc',async(req,res)=>{
const query_Data = req.body.query_Data
const new_Data = req.body.new_Data
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(db_name);
var myquery = query_Data;
var newvalues = { $set: new_Data };
dbo.collection(collection_name).updateOne(myquery, newvalues, function(err, resp) {
if (err) throw err;
res.send({resp})
db.close();
});
});
})
// Delete specific data in the database
app.delete('/mc',async(req,res)=>{
const query_Data = req.body.query_Data
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db(db_name);
var myobj = query_Data;
dbo.collection(collection_name).deleteOne(myobj, function(err, resp) {
if (err) throw err;
res.send({resp})
db.close();
});
});
})
// create the server
app.listen(port,()=>{
console.log("Our app is running on port",port)
})