-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
131 lines (108 loc) · 3.86 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// server.js
// where your node app starts
// init project
const express = require('express');
const process = require('process');
const app = express();
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// reference files needed for our Cassandra database
const database = require('./cassandra');
const connectDatabase = require('./connect-database');
const dateOptions = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short'
};
// Simple in-memory store to hold on to example
// table ids for reference later
const dataStore = [{
type: 'string',
example: '<span class="frontText">Initializing database</span>'
}];
/**
* Initialize the connection to our Apollo or DataStax Enterprise cluster
* and display the elapsed time of initialization
*/
const start = process.hrtime();
connectDatabase.initCaasConnection()
//connectDatabase.initDseConnection()
.then(result => {
const hrtime = process.hrtime(start);
const elapsed = (hrtime[0] + (hrtime[1] / 1e9)).toFixed(3);
console.info(`Completed in ${elapsed}s.`);
const message = '<span class="frontText">' + result + ' in ' + elapsed + 's</span>';
dataStore.push({type: 'string', example:message});
})
/**.then(() => {
console.log('Creating example keyspace');
database.createKeyspace();
})*/
.then(() => {
console.log('Creating example table');
database.createTable();
})
.catch(err => console.error('Error at table initialization', err));
/**
* Setup some basic routes
* http://expressjs.com/en/starter/basic-routing.html
*/
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html')
});
app.get('/dreams', function(request, response) {
const dataStoreSize = dataStore.length;
const last = dataStoreSize - 1;
console.debug('GET dreams are: %d', dataStoreSize);
if (dataStoreSize > 0) {
if (dataStore[last].type == 'data') {
database.getExampleDataById1AndId2(dataStore[last].id1, dataStore[last].id2)
.then(example => {
const row = example.first();
const id1 = row['id1']; // Uuid
const id2 = row['id2'].getDate(); // pulls date from TimeUUID
const txt = row['txt']; // Text/String value
const val = row['val']; // integer
console.log('Retrieved row: %s, %s, %s, %d', id1, id2, txt, val);
const exampleRow = [txt + ' @ ' + id2];
console.log('Example is %s', exampleRow);
})
.catch(err => console.error('Error at /dreams GET', err));
}
response.send(dataStore).reverse;
} else {
response.sendStatus(200);
}
});
// could also use the POST body instead of query string: http://expressjs.com/en/api.html#req.body
app.post("/dreams", function (request, response) {
console.log('POST request is: %s', request.query.dream);
let dreamRequest = request.query.dream;
let id1; let id2;
// Do insert here
database.insertData(dreamRequest)
.then(resultIds => {
id1 = resultIds[0];
id2 = resultIds[1];
const dateString = id2.getDate().toLocaleDateString("en-US", dateOptions);
const exampleRow = [
'<span class="frontText">' + dreamRequest + '</span>' +
'<div class="dateText"><bold>@</bold>' + dateString + '</div>'
];
dataStore.push({type: 'data', id1: id1, id2: id2, txt: dreamRequest, example: exampleRow});
let dataStoreSize = dataStore.length;
console.debug('dataStore push is: ' + dataStore[dataStoreSize - 1].id1)
response.send(dataStore[dataStoreSize - 1]);
//response.sendStatus(200);
})
.catch(err => console.error('Error at /dreams POST', err));
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});