-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebServer.js
54 lines (42 loc) · 1.34 KB
/
webServer.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
"use strict";
/* jshint node: true */
// To start the webserver run `node webServer.js`
var express = require('express');
var app = express();
var AWS = require('aws-sdk');
var cors = require('cors');
var credentials = require('./credentials');
var bodyParser = require('body-parser');
app.use(express.static(__dirname));
app.use(bodyParser.json());
app.use(cors());
AWS.config.update({
accessKeyId: credentials.accessKey,
secretAccessKey: credentials.accessKeySecret,
});
var s3 = new AWS.S3();
app.get('/datasets/list', function(request, response) {
let params = {
Bucket: credentials.bucket,
Key: "metadata.json",
};
s3.getObject(params, function(error, data) {
if (error) {
console.error('Error fetching datasets', error);
response.status(500).send('Error fetching data from S3');
return;
}
if (data.length === 0) {
console.error('Dataset list', 'Data object empty on return from S3');
response.status(500).send('No data returned from S3');
return;
}
// now data is an object that contains the contents in data.Body
var array = JSON.parse(new Buffer(data.Body).toString("utf8"));
response.end(JSON.stringify(array));
});
});
var server = app.listen(9000, function () {
var port = server.address().port;
console.log('Listening at http://localhost:' + port + ' exporting the directory ' + __dirname);
});