This repository has been archived on 2019-10-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tecnologiabanana/node-entry.js
T

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-11-08 18:36:12 +01:00
const express = require('express');
2019-01-14 19:23:29 +01:00
const compression = require('compression')
2018-12-01 03:02:13 +01:00
const bodyParser = require('body-parser');
const low = require('lowdb');
2018-12-02 21:02:12 +01:00
const lodashId = require('lodash-id');
2018-12-01 03:02:13 +01:00
const FileAsync = require('lowdb/adapters/FileAsync');
const app = express(); // app is an instance of express
2018-11-08 18:36:12 +01:00
2019-01-14 19:23:29 +01:00
app.use(compression()); // enables gzip compression
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
2018-12-02 21:02:12 +01:00
2019-01-14 19:23:29 +01:00
app.get('/a', (req, res) => {
res.send(process.env);
});
2018-12-01 03:02:13 +01:00
// Create database instance and start server
// const adapter = new FileAsync(__dirname + '/db.json');
2018-12-02 21:02:12 +01:00
low(new FileAsync(__dirname + '/db.json')) // production
// low(new FileAsync('/tmp/db.json')) // dev
2018-12-01 03:02:13 +01:00
.then(db => {
2018-12-02 21:02:12 +01:00
db._.mixin(lodashId);
2018-12-01 03:02:13 +01:00
// DATABASE ROUTES
2018-11-08 18:36:12 +01:00
2018-12-02 21:02:12 +01:00
// ==============
app.post('/test',(req,res)=>{
2018-12-03 01:22:48 +01:00
// "reason": {
// "fitali": req.body.reason.toString() === "fvitali" ? 1:0,
// "": req.body.reason.toString() === "" ? 1:0,
// "": req.body.reason.toString() === "" ? 1:0,
// }
// try {
// let test = table.updateById(req.body.id, { })
// }
// catch(e) {
// }
res.send(req.body);
});
// ==============
// GET /videotrack/:id?
app.get('/videotrack/:id?', (req, res) => {
const videos = db.get('videos');
req.params.id ? res.send(videos.getById(req.params.id).value()) : res.send(videos.value())
});
// POST /videotrack
app.post('/videotrack', (req, res) => {
const videos = db.get('videos');
const row = videos.getById(req.body.id);
2018-12-02 21:02:12 +01:00
if(row.value()) {
row
.update('timesWatched',(n) => ++n)
.update('lastWatched',() => new Date().toISOString())
.write()
2018-12-03 01:22:48 +01:00
.then(output => res.send(output))
2018-12-02 21:02:12 +01:00
} else {
2018-12-03 01:22:48 +01:00
let newRow = {
2018-12-02 21:02:12 +01:00
"id": req.body.id.toString(),
"timesWatched": 1,
"lastWatched": new Date().toISOString(),
}
2018-12-03 01:22:48 +01:00
videos.push(newRow).last().write().then(output => res.send(output))
2018-12-02 21:02:12 +01:00
}
2018-12-03 01:22:48 +01:00
});
2018-12-02 21:02:12 +01:00
2018-12-01 03:02:13 +01:00
// Set db default values
2018-12-03 01:22:48 +01:00
return db.defaults({ videos: [] }).write();
2018-12-01 03:02:13 +01:00
})
.then(() => {
app.listen(8000, () => console.log('listen on 8000')); // bind to port 8000 as required from specs
});