-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (54 loc) · 1.76 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
/**
* simple query tool for relational databases relying on Sequelize ORM
* a config.json file must provide data source info such as db connection,
* table containing data, where to search and what to return
* ENDPOINTS:
/search?q=terms%20to%20search&type=any|all|full (leave empty for "any")
/columns (all table columns)
/columns/selected (columns to display, set in config)
*/
'use strict';
var express = require("express");
var config = require('./config');
var Search = require('./search');
var utf8 = require('utf8');
// this might come from config ?
var API_ROOT = process.env.API_ROOT || '';
var app = express();
app.use("/", express.static("./public"));
var APIready = Search.init(config);
APIready.then(function() {
console.log("Config ready");
});
// all table columns... should be private??
app.get(API_ROOT + "/columns", (req, res) => {
APIready.then(API => res.json(API.get_columns()));
});
// columns that will be visible to frontend result
app.get(API_ROOT + "/columns/selected", (req, res) => {
APIready.then(API => res.json(API.get_columns_selected()));
});
// search itself
app.get(API_ROOT + "/search", (req, res) => {
APIready.then(API => {
//console.log(search);
API.get_search(req.query).then(function(result) {
res.type('json');
let response = JSON.stringify(result, utf8decode);
res.send(response);
});
});
});
const PORT = process.env.PORT || 3000;
console.log("Listening on http://localhost:" + PORT);
console.log("API Root: " + API_ROOT);
app.listen(PORT);
function utf8decode(key, value) {
if (typeof value !== 'string') return value;
try {
value = utf8.decode(value);
} catch(e) {
console.log(e);
}
return value;
}