🛎️ Checkpoint

./server.js:7435365/3113
This commit is contained in:
Glitch (hello-express) 2019-12-20 23:12:05 +00:00
parent a1aebd61ee
commit 06aad43cf3

View File

@ -1,15 +1,11 @@
// server.js // Initialize project
// where your node app starts const express = require('express');
const Sequelize = require('sequelize');
// init project
var express = require('express');
var Sequelize = require('sequelize');
const Op = Sequelize.Op const Op = Sequelize.Op
var request = require('request'); // TEMPORARY! Used for import function. //const request = require('request'); // TEMPORARY! Used for import function.
var app = express(); const app = express();
// setup a new database // Setup database, using credentials set in .env
// using database credentials set in .env
var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, { var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, {
host: '0.0.0.0', host: '0.0.0.0',
dialect: 'sqlite', dialect: 'sqlite',
@ -24,12 +20,12 @@ var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PA
}); });
var Readings; var Readings;
// authenticate with the database // Authenticate with the database
sequelize.authenticate() sequelize.authenticate()
.then(function(err) { .then(function(err) {
console.log('Connection has been established successfully.'); console.log('Connection has been established successfully.');
// define a new table 'readings' // Define 'readings' table structure
Readings = sequelize.define('readings', { Readings = sequelize.define('readings', {
timestamp: { timestamp: {
type: Sequelize.DATE type: Sequelize.DATE
@ -39,21 +35,19 @@ sequelize.authenticate()
} }
}); });
// Read data
Readings.sync(); Readings.sync();
}) })
.catch(function (err) { .catch(function (err) {
console.log('Unable to connect to the database: ', err); console.log('Unable to connect to the database: ', err);
}); });
// we've started you off with Express, // Handle requests for the root page by serving the static index.html from the views folder
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function(req, res) { app.get("/", function(req, res) {
res.sendFile(__dirname + "/views/index.html"); res.sendFile(__dirname + "/views/index.html");
}); });
// Handle requests to post a new reading // Handle requests to /newreading by posting a new reading to the database
app.get('/newreading', (req, res) => { app.get('/newreading', (req, res) => {
// Filter out requests that don't include the secret key, or don't contain the required data // Filter out requests that don't include the secret key, or don't contain the required data
if (req.query.s != process.env.SECRET || !RegExp('^\\d*$').test(req.query.v)) return res.status(400).send('Bad Request').end(); if (req.query.s != process.env.SECRET || !RegExp('^\\d*$').test(req.query.v)) return res.status(400).send('Bad Request').end();
@ -86,38 +80,26 @@ app.get('/getdata', (req, res) => {
}) })
}); });
/* // Handle requests to /cleanup by deleting any data older than 90 days from the datastore
const query = datastore app.get('/cleanup', (req, res) => {
.createQuery('entry') // SECURITY CHECK FOR THE CORRECT CRONJOB HEADER GOES HERE!!
.filter('timestamp', '>=', new Date(req.query.from))
.filter('timestamp', '<', new Date(req.query.to))
.order('timestamp', { descending: true });
datastore.runQuery(query, function(err, ent) {
res
.status(200)
.send(ent)
.end()
})
var fromDate = new Date('2019-12-01T00:00:00.000Z')
//return res.status(200).send(fromDate).end();
Readings.findAll({ var before = new Date();
attributes: ['timestamp', 'reading'], before.setDate(before.getDate()-91);
before.setHours(0);
before.setMinutes(0);
before.setSeconds(0);
Readings.destroy({
where: { where: {
timestamp: { timestamp: {
[Op.lte]: fromDate [Op.lt]: before
} }
}, }
order: [
['timestamp', 'DESC']
]
}).then((result) => { }).then((result) => {
res.status(200).send(result).end(); res.status(200).send(result).end();
}) });
});*/ });
/* /*
app.get('/import', (req, res) => { app.get('/import', (req, res) => {