-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (57 loc) · 1.71 KB
/
server.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
66
const express = require('express');
var Sequelize = require('sequelize');
const app = express();
const port = process.env.PORT || 5000;
app.use(express.json());
app.use(express.urlencoded());
app.use('/', express.static(`${__dirname}/client/build`));
var sequelize = new Sequelize('database', process.env.DB_USER, process.env.DB_PASS, {
host: '0.0.0.0',
dialect: 'sqlite',
pool: {
max: 5,
min: 0,
idle: 10000
},
// Security note: the database is saved to the file `database.sqlite` on the local filesystem. It's deliberately placed in the `.data` directory
// which doesn't get copied if someone remixes the project.
storage: 'data/database.sqlite'
});
sequelize.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
// define a new table 'users'
History = sequelize.define('history', {
week_of: {
type: Sequelize.STRING
},
data: {
type: Sequelize.JSON
}
});
setup();
})
.catch(function (err) {
console.log('Unable to connect to the database: ', err);
});
function setup(){
History.sync()
.then(function(d){
console.log("synced", d)
});
}
app.post('/api/history', (req, res) => {
History.create({ week_of: req.body['week_of'], data: req.body['data']}).then( ()=>{
res.send({ status:'saved', week_of: req.body['week_of'], data: req.body['data'] });
} ).catch((err) => {
res.send({error: err})
})
});
app.get('/api/history', (req, res) => {
let week_of = req.query.week_of
console.log("date: ", week_of)
History.findOne({where:{week_of: week_of}}).then(history => {
res.send(history)
})
})
app.listen(port, () => console.log(`Listening on port ${port}`));