This repository was archived by the owner on Nov 9, 2022. It is now read-only.
forked from fxue/bitemporal-demo
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.js
More file actions
106 lines (82 loc) · 2.54 KB
/
app.js
File metadata and controls
106 lines (82 loc) · 2.54 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
91
92
93
94
95
96
97
98
99
100
101
102
var request = require('request');
/* express set up */
var express = require('express');
var app = express();
app.engine('.html', require('ejs').__express);
app.set('views', __dirname);
app.set('view engine', 'html');
/* MarkLogic set up */
var marklogic = require('marklogic');
var conn;
try {
conn = require('./local-env.js').connection;
} catch (e) {
conn = require('./env.js').connection;
}
var db = marklogic.createDatabaseClient(conn);
var q = marklogic.queryBuilder;
// Proxy requests to the MarkLogic REST API
function proxy(req, port, res) {
var queryString = req.originalUrl.split('?')[1];
console.log(req.method + ' ' + req.path + ' proxied to ' + conn.host + ':' + port + req.path + (queryString ? '?' + queryString : ''));
var headers = req.headers;
if (port === 8002) {
// TODO: The /manage/v2/databases/Documents/temporal/collections has been
// giving an HTML useless response to this when queried from jQuery. After
// switching user-agent to pretend to be curl, it's fine. Not clear why.
headers['user-agent'] = 'curl/7.37.1';
}
req.auth = {
user: conn.user,
password: conn.password,
sendImmediately: false
};
req.pipe(request('http://' + conn.host + ':' + port + req.originalUrl)).pipe(res);
}
function getData(collection, res) {
// query database for documents to be shown
// use localhost:3000/data?collection=***
var query = db.documents.query(q.where(q.collection(collection)));
query.result(function(r){
var i;
// get result and log all uris
console.log('GET DOCS from collection = ' + collection);
console.log('result =');
for (i=0; i<r.length; i++) {
console.log(r[i].uri);
}
return r;
}).
then(function(r){
// send json back to http response
console.log('rendering....');
res.json(r);
});
}
/* use Express as http server */
app.use('/public', express.static(__dirname+'/public'));
app.get('/data', function(req, res) {
getData( 'addr.json' , res);
});
app.get('/data/:collection', function(req, res) {
getData(req.param('collection'), res);
});
app.all('/v1*', function(req, res){
proxy(req, conn.port, res);
});
app.all('/manage/*', function(req, res){
proxy(req, 8002, res);
});
app.get('/delete', function(req, res) {
// delete temporal document
// TODO add your code here...
});
app.get('/', function(req, res) {
res.sendfile(__dirname + '/search.html');
});
app.get('/view', function(req, res) {
res.sendfile(__dirname + '/view.html');
});
/* start listening */
app.listen(3000);
console.log('listening on port 3000');