2018-11-08 18:36:12 +01:00
|
|
|
const express = require('express');
|
2018-12-01 03:02:13 +01:00
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
|
const low = require('lowdb');
|
|
|
|
|
const FileAsync = require('lowdb/adapters/FileAsync');
|
|
|
|
|
const app = express(); // app is an instance of express
|
2018-11-08 18:36:12 +01:00
|
|
|
|
2018-12-01 03:02:13 +01:00
|
|
|
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
|
|
|
|
|
app.use(bodyParser.json()) // parse application/json
|
|
|
|
|
app.use(express.static(__dirname + '/build')); // serve static build site
|
|
|
|
|
app.post('/test',(req,res)=>{
|
|
|
|
|
res.send(req.body);
|
|
|
|
|
})
|
2018-11-08 18:36:12 +01:00
|
|
|
|
2018-12-01 03:02:13 +01:00
|
|
|
// Create database instance and start server
|
|
|
|
|
// const adapter = new FileAsync(__dirname + '/db.json');
|
|
|
|
|
low(new FileAsync(__dirname + '/db.json'))
|
|
|
|
|
.then(db => {
|
|
|
|
|
// DATABASE ROUTES
|
2018-11-08 18:36:12 +01:00
|
|
|
|
2018-12-01 03:02:13 +01:00
|
|
|
// GET /videolog
|
|
|
|
|
app.get('/videolog', (req, res) => {
|
|
|
|
|
const post = db
|
|
|
|
|
.get('videos')
|
|
|
|
|
// .find({ id: req.params.id })
|
|
|
|
|
.value();
|
|
|
|
|
res.send(post);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// POST /videolog
|
|
|
|
|
app.post('/videolog', (req, res) => {
|
|
|
|
|
db.get('videos')
|
|
|
|
|
.push(req.body)
|
|
|
|
|
.last()
|
|
|
|
|
.update('timesWatched', n => n + 1 )
|
|
|
|
|
.write()
|
|
|
|
|
.then(post => res.send(post)); // non funziona minimamente lol
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Set db default values
|
|
|
|
|
return db
|
|
|
|
|
.defaults({
|
|
|
|
|
videos: [
|
|
|
|
|
// {
|
|
|
|
|
// videoId: '',
|
|
|
|
|
// timesWatched: 0,
|
|
|
|
|
// prevalentReason: '',
|
|
|
|
|
// lastSelected: new Date()
|
|
|
|
|
// }
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
.write();
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs
|
|
|
|
|
});
|