diff --git a/package.json b/package.json index 2ae0db9..4cc9838 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,10 @@ "dependencies": { "body-parser": "^1.14.1", "cors": "^2.7.1", - "express": "^4.13.3" + "express": "^4.13.3", + "tabletop": "^1.4.2" + }, + "devDependencies": { + "morgan": "^1.6.1" } } diff --git a/server.js b/server.js index 8781f7c..442e213 100644 --- a/server.js +++ b/server.js @@ -3,15 +3,43 @@ 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}) + +var globalData = [] // Need to figure out a way not to use a global! app.listen(8080); console.log('Server running on port 8080'); -app.use(bodyParser.urlencoded({ extended: false })); +// app.use(logger('combined', { +// stream: accessLogStream +// })) +// app.use(bodyParser.urlencoded({ extended: false })); app.use(cors()) +app.use(function (req, res, next) { + console.log('starting the middlewarezzz') + var public_spreadsheet_url = + 'https://docs.google.com/spreadsheets/d/1Lsk1dxcfTuF0Qu_a-roO5EEsOy05zUdLYbCg38wovTA/pubhtml?gid=276685053&single=true' + + Tabletop.init( { key: public_spreadsheet_url, + callback: showInfo, + simpleSheet: true, + debug: true + } ) + + function showInfo(data, tabletop) { + console.log("Successfully processed!") + // console.log(data) + globalData = data + next() + } +}) + app.get('/', function(req, res) { res.send('Hello World'); @@ -19,8 +47,12 @@ app.get('/', function(req, res) { // list catalog app.get('/catalog', function(req, res) { - res.json(db.catalog); -}); + console.log('Getting the catalog') + // console.log('Heres your request', req) + + res.json(globalData); +}) + app.get('/catalog/:id', function(req, res) { var id = Number(req.params.id); @@ -39,7 +71,7 @@ app.post('/catalog/new', function(req, res) { db.catalog.push({ id: newid, name: req.body.name, - price: req.body.price, + price: req.body.price, description: req.body.description }); fs.writeFile('./fakedata.json', JSON.stringify(db), function(err) { if (err) throw err;