-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (36 loc) · 1.16 KB
/
Copy pathserver.js
File metadata and controls
44 lines (36 loc) · 1.16 KB
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
// Express Setup
const express = require('express');
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'))
// Knex Setup
const env = process.env.NODE_ENV || 'development';
const config = require('./knexfile')[env];
const db = require('knex')(config);
app.get('/api/runs', (req,res) => {
db('runs').select().from('runs').then(runs => {
res.send(runs);
}).catch(error => {
res.status(500).json({ error });
});
});
app.post('/api/runs', (req,res) => {
db('runs').insert({length:req.body.length,time: req.body.time,personal_notes: req.body.personal_notes,run_on: new Date()}).then(run => {
res.status(200).json({id:run[0]});
}).catch(error => {
console.log(error);
res.status(500).json({ error });
});
});
app.delete('/api/runs/:id', (req,res) => {
let id = parseInt(req.params.id);
db('runs').where('id',id).del().then(runs => {
res.sendStatus(200);
}).catch(error => {
console.log(error);
res.status(500).json({ error });
});
});
app.listen(3001, () => console.log('Server is listening on port 3001!!!'))