-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (90 loc) · 3.39 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var sqlite3 = require('sqlite3').verbose();
DatabaseConnection = new sqlite3.Database('./desire.db');
var MyObject = require('./desire/MyObject.js');
var ObjectDesire = require('./desire/ObjectDesire.js');
var MyDesire = require('./desire/MyDesire.js');
app.use(express.static('public'));
app.use('/js', express.static('desire/js'));
app.use(bodyParser.json());
app.get('/', function (req, res) {
var output = 'Hello World!';
var breakStr = '</br></br>';
output += breakStr;
output += '<a href="http://localhost:3000/queryAllObjects/MyObject">All of My Objects</a>';
output += breakStr;
output += '<a href="http://localhost:3000/queryAllObjects/MyDesire">All of My Desires</a>';
output += breakStr;
output += '<a href="http://localhost:3000/queryAllObjects/ObjectDesire">All Objects of My Desires</a>';
output += breakStr;
output += '<a href="http://localhost:3000/queryObjectByPK/MyObject/1">A Specific Object</a>'
output += breakStr;
output += '<a href="http://localhost:3000/queryObjectByFK/ObjectDesire/MyObject/Id/1">My Desires of a specific Object</a>';
output += breakStr;
res.send(output);
})
app.post('/save/:object', function(req, res){
console.log(req.params.object);
var object = req.body;
console.log(object);
Object.setPrototypeOf(object, eval(req.params.object+'.prototype'));
console.log(object);
object = object.save();
res.send(object);
});
app.post('/delete/:object', function(req, res){
console.log(req.params.object);
var object = req.body;
console.log(object);
Object.setPrototypeOf(object, eval(req.params.object+'.prototype'));
console.log(object);
retObject = object.delete();
res.send(retObject);
});
app.get('/queryObjectByPK/:object/:id', function(req, res){
var obj = eval(req.params.object).queryByPrimaryKeyId(req.params.id);
console.log(obj);
console.log(req.params.object);
console.log(req.params.id);
res.send(obj);
});
app.get('/queryAllObjects/:object', function(req, res){
console.log('all object');
console.log(req.params);
var objArray = eval(req.params.object).queryAll();
console.log(objArray);
console.log(req.params.object);
res.send(objArray);
});
app.get('/pageObjects/:object/:start/:end', function(req, res){
console.log('all object');
console.log(req.params);
var tableName = req.params.object;
var start = parseInt(req.params.start);
var end = parseInt(req.params.end);
var total = eval(req.params.object).queryAllCount().count;
var resultArray = eval(req.params.object).queryPage(start, end);
console.log(req.params.object);
var tablePage = {};
tablePage.table = tableName;
tablePage.total = total;
tablePage.start = (start>total)?total:start;
tablePage.end = (end >= total)?(total):(end);
tablePage.result = resultArray;
res.send(tablePage);
});
app.get('/queryObjectByFK/:object/:fkObject/:fkName/:fkId', function(req, res){
console.log(req.params.object);
console.log(req.params.fkObject);
console.log(req.params.fkName);
console.log(req.params.fkId);
var objArray = eval(req.params.object+'.queryByForeignKey'+req.params.fkObject+req.params.fkName+'('+req.params.fkId+')');
res.send(objArray);
});
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})