-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (50 loc) · 1.69 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
var express = require('express');
var r = require('rethinkdb');
var app = express();
// set the port of our application
// process.env.PORT lets the port be set by Heroku
var port = process.env.PORT || 8000;
// Load config for app
var config = require(__dirname+"/config.js");
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname+'/public'));
// set the home page route
app.get('/', function(req, res) {
// ejs render automatically looks in the views folder
res.sendfile('index.html');
});
app.get('/datasets/nzfapdc/species/:name', function(req, res) {
var connection = null;
r.connect(config.rethinkdb).then(function(conn) {
connection = conn;
return r.table('nzfapdc').filter({"Matched Scientific Name": req.params.name}).run(connection)
}).then(function(cursor) {
return cursor.toArray();
}).then(function(result) {
res.send(JSON.stringify(result));
}).error(handleError(res))
.finally(function(){connection.close()});
});
app.get('/datasets/nzfapdc/dropdown', function(req, res) {
var connection = null;
r.connect(config.rethinkdb).then(function(conn) {
connection = conn;
return r.db('test').table('nzfapdcDropdown').orderBy(r.desc('reduction')).limit(1000).run(connection);
}).then(function(cursor) {
return cursor.toArray();
}).then(function(result) {
res.send(JSON.stringify(result));
}).error(handleError(res))
.finally(function(){connection.close()});
});
app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});
/*
* Send back a 500 error
*/
function handleError(res) {
return function(error) {
res.send(500, {error: error.message});
}
}