diff --git a/node-entry.js b/node-entry.js index c6e9d21..9268cc4 100644 --- a/node-entry.js +++ b/node-entry.js @@ -43,9 +43,22 @@ low(new FileAsync(__dirname + '/db.json')) // production }); // ============== + // OPTIONS /globpop + app.options('/globpop', (req, res) => { + res.set({ + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': ' GET, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': 600 + }) + res.status(200).end(); + }) // GET /globpop app.get('/globpop', (req, res) => { + res.set({ + 'Access-Control-Allow-Origin': '*' + }); req.query.id ? res.send(req.query.id) : res.status(400).send('Bad Request'); }); @@ -74,21 +87,36 @@ low(new FileAsync(__dirname + '/db.json')) // production res.set({ 'Access-Control-Allow-Origin': '*' }); + let setReason = function (reason='Starter', oldObj = {}) { + if (oldObj.hasOwnProperty(reason)) { + ++oldObj[reason]; + return oldObj; + } + else { + Object.defineProperty(oldObj, + reason, { value: 1, enumerable: true, writable: true } + ) + return oldObj; + } + } + const videos = db.get('videos'); const row = videos.getById(req.body.id); if (row.value()) { row .update('timesWatched', (n) => ++n) .update('lastWatched', () => new Date().toISOString()) + .update('reason', (n) => setReason(req.body.reason, n)) .write() - .then(output => res.send(output)) + .then(output => res.send(200, output)) } else { let newRow = { "id": req.body.id.toString(), "timesWatched": 1, "lastWatched": new Date().toISOString(), + "reason": setReason(req.body.reason) } - videos.push(newRow).last().write().then(output => res.send(200,output)) + videos.push(newRow).last().write().then(output => res.send(200, output)) } }); @@ -97,8 +125,8 @@ low(new FileAsync(__dirname + '/db.json')) // production }) .then(() => { app.use(express.static(__dirname + '/build')); // serve static build site - app.get('*', (req,res) =>{ - res.sendFile(__dirname+'/build/index.html'); + app.get('*', (req, res) => { + res.sendFile(__dirname + '/build/index.html'); }); app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs });