-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
51 lines (42 loc) · 1.63 KB
/
index.js
File metadata and controls
51 lines (42 loc) · 1.63 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
const express = require('express')
const bodyParser = require('body-parser')
const { Pool } = require('pg')
const format = require('pg-format')
const _ = require('underscore')
const client = new Pool({
connectionString: process.env.DATABASE_URL,
ssl: true,
})
client.connect()
var app = express()
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json({limit: '50mb'}));
// respond with "hello world" when a GET request is made to the homepage
app.use(express.static('public'))
app.post('/strokes', function (req, res) {
if (req.body.length > 0 && req.body[0].user) console.log (`Recieved ${req.body.length} strokes from ${req.body[0].user}`);
// Insert distinct values
// This joins the values as a string, makes a set from them, casts to an array and then splits back to distanct data points
let insertValues = Array.from(new Set(req.body.map(r => {
try {
return [r.user, Number(r.time), Number(r.keyCode), Number(r.modifiers), r.direction]
} catch(e) {
}
}).filter((i) => i).map(r => r.join(',')))).map(r => r.split(','))
// insertValues = insertValues.filter(i => insertValues.findIndex(x => x.every((e, index) => e != i[index])) < 0)
const insertText = format('INSERT INTO strokes ( user_id, key_time, key_code, modifiers, direction) VALUES %L', insertValues)
client.query(insertText, (err, result) => {
if (err) {
console.log(err.stack)
res.status(500).send(err.stack)
} else {
res.send('success')
}
})
})
app.listen(app.get('port'), function () {
console.log('Log app listening on port ' + app.get('port'))
})