-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (52 loc) · 1.33 KB
/
index.js
File metadata and controls
62 lines (52 loc) · 1.33 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
const cors = require("cors");
const express = require("express");
const bodyParser = require("body-parser");
const mysql = require("mysql");
const getSQL = require("wheresql");
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "query"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(express.static("./"));
const port = "3200";
const host = "localhost";
const server = app.listen(port, host, function () {
console.log("Server is running on port " + port + "...");
console.log(`Open http://${host}:${port} in browser`);
});
const dbConfig = {};
app.post("/api/data/:table", (req, res, next) => {
const {sql, values, error} = getSQL(req.body, dbConfig);
if(error)
return res.status(500).send(error);
con.query(
`SELECT * FROM ${req.params.table} WHERE ${sql}`,
values,
function (err, result) {
if (err)
next(err);
else
res.send(result);
}
);
});
app.get("/api/data/:table/:field/suggest", (req, res, next) => {
con.query(
`SELECT DISTINCT ${req.params.field} FROM ${req.params.table} ORDER BY ${req.params.field} ASC`,
function (err, result, fields) {
if (err)
next(err);
else
res.send(result.map(obj => obj[req.params.field]));
}
);
});