-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
66 lines (59 loc) · 1.54 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//Imports
require('dotenv').config()
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
var Schema = mongoose.Schema;
const winston = require('winston');
const moment = require('moment');
//Globals
const app = express();
const port = process.env.PORT;
const DB_URL = process.env.MONGO_URI;
var db;
//Middleware
app.use(cors());
app.use(express.json())
/*
* Server Initalization
*/
const LOGGER = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'logs/combined.log' })
]
});
mongoose.set('debug', true);
mongoose.connect(DB_URL, {useNewUrlParser: true}).then(connectedDB => {
db = connectedDB;
app.listen(port, () =>
LOGGER.log({
level: 'info',
message: 'Server restarted'
})
);
})
/*
* Primary Routes
*/
app.get('/', function (req, res, next) {
res.status(200).send("OK");
});
var openSchema = new Schema({}, { strict: false });
var Container = mongoose.model('container', openSchema);
app.post('/heightRecord/:containerId', function (req, res, next) {
let timeStampKey = `heightRecords.${moment().unix()}`;
Container.updateOne(
{ _id: req.params.containerId },
{
$push: {
wasteLevels: req.body.distance,
wasteTimes: moment().format("YYYY-MM-DD HH:mm:ss")
}
}
).then(() => {
res.status(200).send("OK");
}).catch(err => {
res.status(500).send(err);
})
});