-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
118 lines (101 loc) · 3.11 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
117
118
var mysql = require('mysql');
const bodyParser=require('body-parser');
const express = require('express');
var nodeExcel = require('excel-export');
const dbcontroller = require ("./dbController.js");
const app = express();
const port = 3000;
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'development'
});
function json2array(json){
var result = [];
var keys = Object.keys(json);
keys.forEach(function(key){
result.push(json[key]);
});
return result;
}
var dataToExcel= [];
connection.connect();
app.use(bodyParser.json());
app.post('/jobs', function (req, res) {
connection.query('insert into jobs (location) values ("'+req.body.text+'")', function (error, results, fields) {
if (error) throw error;
res.send('OK');
});
});
app.get('/jobs', function (req, res) {
connection.query('select * from jobs', function (error, results, fields) {
if (error) throw error;
res.send(results);
});
});
app.get('/jobs/:queryValue', function (req, res) {
var queryArray=req.params.queryValue.split("=");
if(queryArray[0]&&queryArray[1]) {
connection.query('select * from jobs where '+queryArray[0]+'="'+queryArray[1]+'"', function (error, results, fields) {
if (error) throw error;
res.send(results);
});
}
else {
res.send("invalid request");
}
});
app.delete('/jobs/:queryValue', function (req, res) {
var queryArray=req.params.queryValue.split("=");
if(queryArray[0]&&queryArray[1]) {
connection.query('delete from jobs where '+queryArray[0]+'="'+queryArray[1]+'"', function (error, results, fields) {
if (error) throw error;
res.send('OK');
});
}
else {
res.send("invalid request");
}
});
app.patch('/jobs/:queryValue', function (req, res) {
var queryArray=req.params.queryValue.split(",");
if(queryArray[0]&&queryArray[1]&&queryArray[2]&&queryArray[3]) {
connection.query('UPDATE jobs SET '+queryArray[0]+'="'+queryArray[1]+'" WHERE ' +queryArray[2]+'="'+queryArray[3]+'"', function (error, results, fields) {
if (error) throw error;
res.send('OK');
});
}
else {
res.send("invalid request");
}
});
app.get('/Excel', function(req, res){
var conf ={};
conf.name = "mysheet";
conf.cols = [
{
caption:'Location',
type:'string'
},{
caption:'ID',
type:'number'
}];
dbcontroller.getAllUserData().then((data)=>{
console.log(JSON.stringify(data,undefined,2));
for(var i=0;i<data.length;i++) {
dataToExcel.push(json2array(data[i]));
}
conf.rows = dataToExcel;
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
res.end(result, 'binary');
dataToExcel.splice(0, dataToExcel.length);
},(error)=>{
console.log(error);
}
);
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
// connection.end();