-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
90 lines (83 loc) · 3.14 KB
/
Copy pathproxy.js
File metadata and controls
90 lines (83 loc) · 3.14 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
88
89
90
const express = require('express');
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const app = express();
app.use(express.json());
app.use(express.static('.'));
const RDF4J = 'http://localhost:8080/rdf4j-server/repositories/grafexamen';
app.get('/statiuni', async (req, res) => {
const query = `
PREFIX ex: <http://example.org/statiuni/>
SELECT ?nume ?tara ?an WHERE {
?s a ex:Statiune ;
ex:nume ?nume ;
ex:tara ?tara ;
ex:anInaugurare ?an .
}
`;
const response = await fetch(`${RDF4J}?query=${encodeURIComponent(query)}`, {
headers: { 'Accept': 'application/sparql-results+json' }
});
const data = await response.json();
res.json(data);
});
app.get('/plaje', async (req, res) => {
const statiune = req.query.statiune;
const query = `
PREFIX ex: <http://example.org/statiuni/>
SELECT ?numeStatiune ?numePlaja ?lungime ?tip WHERE {
?p a ex:Plaja ;
ex:nume ?numePlaja ;
ex:lungime ?lungime ;
ex:tip ?tip ;
ex:apartineDe ?s .
?s ex:nume ?numeStatiune .
FILTER(?numeStatiune = "${statiune}")
}
`;
const response = await fetch(`${RDF4J}?query=${encodeURIComponent(query)}`, {
headers: { 'Accept': 'application/sparql-results+json' }
});
const data = await response.json();
res.json(data);
});
app.post('/adauga-plaja', async (req, res) => {
const { numePlaja, lungime, tip, numeStatiune } = req.body;
const update = `
PREFIX ex: <http://example.org/statiuni/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT DATA {
ex:${numePlaja.replace(/\s+/g, '')} a ex:Plaja ;
ex:nume "${numePlaja}" ;
ex:lungime "${lungime}"^^xsd:decimal ;
ex:tip "${tip}" ;
ex:apartineDe ?s .
}
`;
const statiuneQuery = `
PREFIX ex: <http://example.org/statiuni/>
SELECT ?s WHERE { ?s ex:nume "${numeStatiune}" . }
`;
const statiuneResp = await fetch(`${RDF4J}?query=${encodeURIComponent(statiuneQuery)}`, {
headers: { 'Accept': 'application/sparql-results+json' }
});
const statiuneData = await statiuneResp.json();
const statiuneURI = statiuneData.results.bindings[0].s.value;
const insertQuery = `
PREFIX ex: <http://example.org/statiuni/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT DATA {
ex:${numePlaja.replace(/\s+/g, '')} a ex:Plaja ;
ex:nume "${numePlaja}" ;
ex:lungime "${lungime}"^^xsd:decimal ;
ex:tip "${tip}" ;
ex:apartineDe <${statiuneURI}> .
}
`;
await fetch(`${RDF4J}/statements`, {
method: 'POST',
headers: { 'Content-Type': 'application/sparql-update' },
body: insertQuery
});
res.json({ succes: true });
});
app.listen(5000, () => console.log('Proxy pornit pe http://localhost:5000'));