forked from kxjdora/jsonld-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.2.js
77 lines (62 loc) · 2.19 KB
/
index.2.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
'use strict';
var shacler = require('./lib/shacler.js');
var shacl = require('./lib/checker.js');
var superagent=require("superagent");
const utils = require('./lib/utils.js');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();
const fs = require('fs');
const request = require('request');
const sfile= __dirname + "/public/data/schema.json"
const dfile= __dirname + "/public/data/data.json";
const $rdf = require('rdflib');
const RDF = $rdf.Namespace('https://www.w3.org/1999/02/22-rdf-syntax-ns#')
const sh = $rdf.Namespace('https://www.w3.org/ns/shacl#');
const a = RDF('type')
const why = "https://tr.com"
var surl = __dirname + "/public/data/s.json"; //新建一个本地文件
var slink = "https://127.0.0.1:3000/data/schema.json";
const s_stream = fs.createWriteStream(surl); //创建surl文件的写入流
const s_req = superagent.get(slink); //获得slink的超级代理
s_req.pipe(s_stream); //把slink中的内容写到本地文件surl中
var durl = __dirname + "/public/data/d.json";
var dlink = "https://127.0.0.1:3000/data/data.json";
const d_stream = fs.createWriteStream(durl);
const d_req = superagent.get(dlink);
d_req.pipe(d_stream);
app.use(express.static(path.join(__dirname, 'public')))
app.use(bodyParser.urlencoded({
extended: true
}));
//body parser
app.use(bodyParser.json());
function fread(path) {
return fs.readFileSync(path, 'utf-8');
}
function validate (res) {
var sbody = fread(surl);
var dbody = fread(durl);
var g = new $rdf.graph();
var s = new shacl(g, undefined, undefined,undefined);
$rdf.parse(sbody, g, why, 'application/ld+json', function(err, expand){
s.addGraph(expand);
$rdf.parse(dbody, g, why, 'application/ld+json', function(err, expand){
s.addGraph(expand)
s.execute();
});
});
};
function test(res) {
validate(res);
res.send("look at result in console please!");
};
//Test Shacl Validator
app.get('/t', function (req, res) {
test(res);
})
//Start web server
app.listen((process.env.PORT || 3000), function () {
console.log("Server up and listening");
});