-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
251 lines (205 loc) · 5.97 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const express = require('express');
const path = require('path');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const nodeInfo = require('./nodeInfo').nodeInfo;
const dbpedia = require('./dbpedia/dbpedia');
/**
* Starts the express.js API and the DBpedia process
*/
app.use(express.static(path.join(__dirname, 'frontend/build')));
console.log("Waiting for express API...");
const server = app.listen(process.env.PORT || 8080, () => {
console.log(`Done! Express running → PORT ${server.address().port}`);
dbpedia.start();
});
/**
* Allow CORS
*/
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
next();
});
/**
* Searches for an entity on DBpedia
*/
app.get('/search', async function(req, res) {
const filter = req.query.filter.toLowerCase();
let queryStr = req.query.queryStr;
if (queryStr === undefined || queryStr === "") {
res.status(404);
res.send(`No results for ${queryStr} of type ${filter} were found.`);
return;
}
queryStr = parseInput(queryStr);
if(nodeInfo.hasOwnProperty(filter)) {
const originalStr = parseOutput(queryStr);
dbpedia.values(queryStr, undefined, (result) => {
if (result.error !== undefined) {
res.status(400);
res.send(result);
}
else if(checkInitialNodeType(result.results.bindings, nodeInfo[filter].validation.toUpperCase())) {
res.status(200);
res.send(createNode(filter, originalStr));
}
else {
res.status(404);
res.send(`No results for ${originalStr} of type ${filter} were found.`);
}
});
}
else {
res.status(400);
res.send(`Unknown filter ${filter}`);
}
});
/**
* Redirects a values request from DBpedia
*/
app.get('/values', function(req, res) {
let {entities, properties} = req.query;
if (entities === undefined || entities === "") {
res.status(404);
res.send({error: `No results for ${entities}`});
return;
}
entities = parseInput(entities);
if (entities != undefined) {
dbpedia.values(entities, properties, (result) => {
if (result.error !== undefined) {
res.status(400);
res.send({result});
}
else
if (result.results === undefined || result.results.bindings === undefined ||
result.results.bindings.length == 0){
res.status(404);
res.send({error: `No results for ${req.query.entities}`});
}
else {
res.status(200);
res.send(result);
}
});
}
else {
res.status(400);
res.send({});
}
});
/**
* Redirects an entities request from DBpedia
*/
app.get('/entities', function(req, res) {
let {value, filter, ofilter} = req.query;
value = parseInput(value);
if (value != undefined) {
dbpedia.entities(value, filter, ofilter, (result) => {
if (result.error !== undefined) {
res.status(400);
res.send(result);
}
else if (result.results.bindings.length == 0){
res.status(400);
res.send({error: `No results for ${req.query.value} were found.`});
}
else {
res.status(200);
res.send(result);
}
});
}
else {
res.status(400);
res.send({});
}
});
/**
* Exports node types and filters
*/
app.get('/nodeInfo', function(req, res) {
res.status(200);
res.send(nodeInfo);
});
/**
* Retrieves each node type filters
*/
app.get('/filters', function(req, res) {
let classes = {};
for(const [type, info] of Object.entries(nodeInfo)) {
let filters = [];
for(const filter of info.filters)
filters.push(filter.name);
classes[type] = filters;
}
res.status(200);
res.send(JSON.stringify(classes));
});
app.get('*', (req,res) =>{
res.sendFile(path.join(__dirname+'/frontend/build/index.html'));
});
/**
* Checks the node for a specific type
*/
function checkInitialNodeType(bindings, validation) {
let args = validation.split(",");
for (let binding of bindings) {
const value = binding['values'].value.toUpperCase();
if (args.length == 0) {
return true;
}
for (let i = 0; i < args.length; i++) {
const validationKeyword = args[i];
if(value.includes(validationKeyword)) {
// console.log("validationKeyword = " + validationKeyword + ", value = " + value);
args.splice(i, 1);
break;
}
}
}
return false;
}
/**
* Detects a node type
*/
function detectNodeType(bindings) {
for (let binding of bindings) {
binding = binding.values.value.toUpperCase();
for (let nodeType in nodeInfo) {
if (binding.includes(nodeInfo[nodeType].validation.toUpperCase())) {
return nodeType;
}
}
}
return "null";
}
/**
* Creates a node object
*/
function createNode(nodeType, name) {
return {
id: name,
type: nodeType,
};
}
/**
* Capitalizes first letter and replaces spaces with underscores
*/
function parseInput(queryStr) {
if (queryStr !== "") {
//DBpedia resources start with capital letters
queryStr = queryStr[0].toUpperCase() + queryStr.slice(1);
//DBpedia resources have _ instead of spaces
queryStr = queryStr.replace(/\s/g, '_');
}
return queryStr;
}
/**
* Replaces underscores with spaces
*/
function parseOutput(queryStr) {
return queryStr.replace(/_/g, ' ');
}