-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
134 lines (118 loc) · 3.4 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
132
133
134
const path = require('path')
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const PORT = process.env.PORT || 8080
const randomWords = require('random-words')
const db = require('./db/db')
const {
incrementLetter,
getAllLetters,
incrementTransition,
getAllTransitions,
} = require('./db/model')
const { median } = require('./util/util')
const { each: asyncEach } = require('async')
// using webpack-dev-server and middleware in development environment
if(process.env.NODE_ENV !== 'production') {
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const webpack = require('webpack')
const config = require('./webpack.config')
const compiler = webpack(config)
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: config.output.publicPath }))
app.use(webpackHotMiddleware(compiler))
}
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'dist')))
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
app.post('/api/dwell', (req, res) => {
const dwellData = req.body
asyncEach(Object.keys(dwellData), (key, outercb) => {
asyncEach(dwellData[key], (time, innercb) => {
incrementLetter(key, time, innercb)
}, (err) => {
if (err) {
console.log(err)
} else {
outercb()
}
})
}, (err) => {
if (err) {
res.sendStatus(500)
} else {
res.sendStatus(201)
}
})
})
app.post('/api/transition', (req, res) => {
const dwellData = req.body
asyncEach(Object.keys(dwellData), (from, outercb) => {
asyncEach(Object.keys(dwellData[from]), (to, middlecb) => {
asyncEach(dwellData[from][to], (time, innercb) => {
incrementTransition(from, to, time, innercb)
}, err => err ? console.log(err) : middlecb())
}, err => err ? console.log(err) : outercb())
}, (err) => {
if (err) {
res.sendStatus(500)
} else {
res.sendStatus(201)
}
})
})
app.get('/api/keyboard', (req, res) => {
getAllLetters((err, result) => {
const medians = Object.keys(result)
.reduce((mediansObject, char) => {
mediansObject[char] = median(result[char])
return mediansObject
}, {})
if (err) {
res.sendStatus(500)
} else {
res.status(200).send(medians)
}
})
})
app.get('/api/transition', (req, res) => {
getAllTransitions((err, result) => {
const medians = {}
for (let fromKey in result) {
for (let toKey in result[fromKey]) {
medians[fromKey] = medians[fromKey] || {}
medians[fromKey][toKey] = median(result[fromKey][toKey])
}
}
if (err) {
res.sendStatus(500)
} else {
res.status(200).send(medians)
}
})
})
app.get('/api/prompt', (req, res) => {
const prompt = randomWords({ exactly: 50, join: ' ' })
res.send(prompt)
})
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
db.connect(function(err) {
if (err) {
console.log('Unable to connect to Mongo.')
process.exit(1)
} else {
app.listen(PORT, (error) => {
if (error) {
console.error(error)
} else {
console.info("==> 🌎 Listening on port %s. Visit http://localhost:%s/ in your browser.", PORT, PORT)
}
})
}
})