🙇🎫 Checkpoint
./server.js:7435365/4061
This commit is contained in:
parent
7a9e6b22c5
commit
3e2ef29648
79
server.js
79
server.js
@ -4,7 +4,7 @@
|
|||||||
// init project
|
// init project
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var Sequelize = require('sequelize');
|
var Sequelize = require('sequelize');
|
||||||
var request = require('request');
|
var request = require('request'); // TEMPORARY! Used for import function.
|
||||||
var app = express();
|
var app = express();
|
||||||
|
|
||||||
// setup a new database
|
// setup a new database
|
||||||
@ -27,6 +27,7 @@ var Readings;
|
|||||||
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 a new table 'readings'
|
||||||
Readings = sequelize.define('readings', {
|
Readings = sequelize.define('readings', {
|
||||||
timestamp: {
|
timestamp: {
|
||||||
@ -51,6 +52,58 @@ app.get("/", function(req, res) {
|
|||||||
res.sendFile(__dirname + "/views/index.html");
|
res.sendFile(__dirname + "/views/index.html");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle requests to post a new reading
|
||||||
|
app.get('/newreading', (req, res) => {
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
Readings.create({ timestamp: new Date(), reading: req.query.v });
|
||||||
|
res.status(200).send('Reading received: ' + req.query.v).end();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle requests for /getdata by returning a JSON object of data for the relevant time period
|
||||||
|
app.get('/getdata', (req, res) => {
|
||||||
|
const iso8601 = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/
|
||||||
|
|
||||||
|
/*
|
||||||
|
if (!req.query.from || !req.query.to || !RegExp(iso8601).test(req.query.from) || !RegExp(iso8601).test(req.query.to)) {
|
||||||
|
res.status(400).send('Bad Request').end();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const query = datastore
|
||||||
|
.createQuery('entry')
|
||||||
|
.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()
|
||||||
|
})
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// <TEMP>
|
||||||
|
app.get('/import', (req, res) => {
|
||||||
|
request({ url: 'https://www.jasonsplant.ml/extract', json: true }, (error, {body}) => {
|
||||||
|
if (error) return;
|
||||||
|
else {
|
||||||
|
var arr=[];
|
||||||
|
body.forEach((item) => {
|
||||||
|
Readings.create({ timestamp: new Date(item.timestamp), reading: item.reading });
|
||||||
|
})
|
||||||
|
|
||||||
|
res.status(200).send('OK').end();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
app.get("/data", function (request, response) {
|
app.get("/data", function (request, response) {
|
||||||
var result = [];
|
var result = [];
|
||||||
Readings.findAll().then(function(reading) { // find all entries in the users tables
|
Readings.findAll().then(function(reading) { // find all entries in the users tables
|
||||||
@ -61,27 +114,11 @@ app.get("/data", function (request, response) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle requests to post a new reading
|
app.get('/reset', (req, res) => {
|
||||||
app.get('/newreading', (req, res) => {
|
Readings.sync({ force: true });
|
||||||
// Filter out requests that don't include the secret key, or don't contain the required data
|
res.status(200).send('OK').end();
|
||||||
if (req.query.s != process.env.SECRET || !RegExp('^\\d*$').test(req.query.v)) return res.status(400).send('Bad Request').end();
|
|
||||||
|
|
||||||
Readings.create({ timestamp: new Date(), reading: req.query.v });
|
|
||||||
res.status(200).send('Reading received: ' + req.query.v).end();
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/import', (req, res) => {
|
|
||||||
request({ url: 'https://www.jasonsplant.ml/extract', json: true }, (error, {body}) => {
|
|
||||||
if (error) return;
|
|
||||||
else {
|
|
||||||
body.foreach((item) => {
|
|
||||||
console.log(item)
|
|
||||||
})
|
|
||||||
|
|
||||||
res.status(200).send(body).end();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
// </TEMP>
|
||||||
|
|
||||||
// listen for requests :)
|
// listen for requests :)
|
||||||
const listener = app.listen(process.env.PORT, function() {
|
const listener = app.listen(process.env.PORT, function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user