forked from skuttleman/boCHA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (94 loc) · 3.1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var express = require('express');
var db = require('./fakedata.json');
var bodyParser = require('body-parser');
var fs = require('fs');
var cors = require('cors')
var Tabletop = require('tabletop')
var logger = require('morgan')
var app = express();
var accessLogStream =
fs.createWriteStream(__dirname + '/access.log', {flags: 'a', verbose: true})
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors())
app.use('/catalog', function (req, res, next) {
var public_spreadsheet_url =
'https://docs.google.com/spreadsheets/d/1xMW98m2-Y8rrbmACA9l1fUT5jb4tKvrRBCSWvH28guw/pubhtml?gid=1526253182&single=true'
Tabletop.init({ key: public_spreadsheet_url,
callback: showInfo,
simpleSheet: true,
debug: false
})
function showInfo(data, tabletop) {
// Save the data to a flat file on the local server
fs.writeFile('./catalog.json', JSON.stringify(data), function(err) {
if (err) throw err;
else console.log('Successfully wrote google sheet data to catalog.json.')
}) // fs.writeFile
// Append the google sheet data to the response.
// This is then passed to the routes, where it can be served to the user.
res.tabledata = data
next()
} // showInfo declaration
}) // app.use
app.get('/', function(req, res) {
res.send('Hello World');
});
// list catalog
app.get('/catalog', function(req, res) {
console.log('Getting the catalog')
// console.log('Heres your request', req)
res.json(res.tabledata);
})
app.post('/orders', function(request, response) {
console.log(request.body)
response.send('Order received.')
})
// app.get('/catalog/:id', function(req, res) {
// var id = Number(req.params.id);
// for (var i = 0; i < db.catalog.length; i++) {
// if (db.catalog[i].id === id) {
// var item = db.catalog[i], i = db.catalog.length;
// }
// }
// if (item) res.json(item);
// else res.sendStatus(404);
// });
// app.post('/catalog/new', function(req, res) {
// var newid = db.catalog[db.catalog.length - 1].id + 1;
// if (req.body.name && req.body.description) {
// db.catalog.push({
// id: newid,
// name: req.body.name,
// price: req.body.price,
// description: req.body.description });
// fs.writeFile('./fakedata.json', JSON.stringify(db), function(err) {
// if (err) throw err;
// else {
// console.log('success')
// res.send(true);
// }
// });
// } else {
// res.send(false);
// }
// });
// app.delete('/catalog/:id', function(req, res) {
// var id = Number(req.params.id);
// var found = false;
// for (var i = 0; i < db.catalog.length; i++) {
// if (db.catalog[i].id === id) {
// found = true;
// db.catalog.splice(i, 1);
// fs.writeFile('./fakedata.json', JSON.stringify(db), function(err) {
// if (err) {
// res.send(false);
// }
// else res.send(true);
// });
// i = db.catalog.length;
// }
// }
// if (!found) res.send(false);
// });
app.listen(process.env.PORT || 5000);
console.log('Server running on port ', process.env.PORT || 5000);