-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
87 lines (55 loc) · 1.86 KB
/
app.js
File metadata and controls
87 lines (55 loc) · 1.86 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
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
//Necessary imports
const express = require('express');
const app = express();
const qh = require('./queryHandler');
const {Client} = require('pg');
let toSql;
const PORT = 5800;
//Instance of client that is used for acquiring desired info from database
const client = new Client({
user: 'postgres',
host: 'postgres',
database: 'geogis',
password: 'rndpassword',
port: 5432,
})
//Logging the process to the console
app.use('/', (req, res, next) => {
console.log("Request received!");
next();
})
app.use('/', express.static('public'));
app.use ('/response', (req, res, next) => {
console.log("Request passed.");
next();
})
client.connect(err => {
if (err) {
console.error("Connection failed, restart the application.", err.stack)
} else {
console.log('Connected!')
}
});
//Handling the request
app.get('/response', (req, res, next) => {
const isEmpty = (value) => value === ""; //checks whether the input query is empty
if (Object.values(req.query).every(isEmpty)) {
res.send("Please input at least one value!")
} else {
let toSql = qh.queryHandler(req.query.ul_name, req.query.label); //transforms the query into SQL statement
let jsonResponse;
client.query(toSql, (err, response) => { //passes the SQL statement to the database
console.log(toSql)
if (err) {
console.log(err);
} else {
jsonResponse = (qh.responseHandler(response.rows));
//res.sendFile(__dirname+'/public/landing.html');
res.send(jsonResponse); //returns a respons in JSON format
};
})
}
})
app.listen(PORT, () => {
console.log('Server started on port ' + PORT);
})