-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
99 lines (85 loc) · 2.59 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
var express = require("express"),
path = require("path");
var app = express();
var http = require('http');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myproject', { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
console.log("connected...");
});
// console.log(path.join(__dirname,"/public"));
const staticPath = path.join(__dirname, "/public");
app.use(express.static(staticPath));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.send("hello..");
});
//rest service
app.get('/getUsers', function (req, res) {
db.collection('sparkbank').find({}).toArray(function (err, sparkbank) {
if (err) {
console.log(err);
}
else {
res.json(sparkbank);
}
});
});
app.post('/insertUser', function (req, res) {
var jsonData = JSON.parse(req.body.mydata);
db.collection('history').insertOne(jsonData, function (err, records) {
if (err) {
console.log(err);
}
else {
res.end('User saved');
}
})
});
app.post('/addCustomer', function (req, res) {
var jsonData = JSON.parse(req.body.add_data);
db.collection('sparkbank').insertOne(jsonData, function (err, records) {
if (err) {
console.log(err);
}
else {
res.end('User saved');
}
})
});
app.put('/updateUser', function (req, res) {
var jsonDataUpdate = JSON.parse(req.body.updatedata);
var FromId = { email: jsonDataUpdate.idFrom };
var ToId = { email: jsonDataUpdate.idTo };
var Frombalance = { $set: { balance: jsonDataUpdate.blFrom } };
var Tobalance = { $set: { balance: jsonDataUpdate.blTo } };
db.collection('sparkbank').updateOne(FromId, Frombalance, function (err, records) {
if (err) {
console.log(err);
}
else {
db.collection('sparkbank').updateOne(ToId, Tobalance, function (err, records) {
if (err) {
console.log(err);
}
else {
res.end('User Updated');
}
})
}
})
});
app.get('/getHistory', function (req, res) {
db.collection('history').find({}).toArray(function (err, history) {
if (err) {
console.log(err);
}
else {
res.json(history);
}
});
});
http.createServer(app).listen(2800);