-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
116 lines (91 loc) · 2.36 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
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
const express = require ('express')
const app =express()
const MongoClient = require('mongodb').MongoClient
/*
app.listen (3001,function(){
console.log (" hello world")
})
*/
var db
MongoClient.connect('mongodb://tester123:tester123@ds145223.mlab.com:45223/blockchain_test', (err, database) => {
if (err) return console.log(err)
db = database
app.listen(3000, () => {
console.log('listening on 3000')
})
db.createCollection("blockchain_test_collection");
db.collection('blockchain_test_collection').createIndex({"name":1},{unique:true});
})
app.get('/seekAll', function(req, res)
{
db.collection('blockchain_test_collection').find().toArray((err, result) => {
if (err)
{
return console.log(err);
}
res.send({blockchain_test_collection: result})
})
})
app.get('/GetCount', function(req, res)
{
db.collection('blockchain_test_collection').count((err, result) => {
if (err)
{
return console.log(err);
}
res.send({blockchain_test_collection: result})
})
})
app.get('/InsertUser', function(req, res) {
var queryName=req.query.name;
var address=req.query.address;
//db.collection('testcol1234').insert({"name" : queryName, "Address" : address });
db.collection('blockchain_test_collection').save({name: queryName, "Address" : address }, (err, result) => {
if (err)
{
//console.log(err); // for debug
res.send(false);
}
else
{
//console.log('saved to database'); //for debug
res.send(true);
}
})
})
app.get('/ValidateUser', function(req, res) {
//res.send('Hello hello World')
var userID=req.query.userName;
db.collection('blockchain_test_collection').find({name:userID}).toArray((err, result) => {
if (err)
{
return console.log(err);
}
if(Object.keys(result).length == 0)
{
res.send(false);
}
else
{
res.send(true);
}
})
})
app.get('/GetAddress', function(req, res) {
//res.send('Hello hello World')
var userID=req.query.userName;
db.collection('blockchain_test_collection').find({name:userID}).toArray((err, result) => {
if (err)
{
return console.log(err);
}
if(Object.keys(result).length == 0)
{
res.send("User does not exist");
}
else
{
res.send(result[0].Address);
}
})
})