Skip to content

Commit

Permalink
Use the tabletop module to pull data from a Google spreadsheet. Serve…
Browse files Browse the repository at this point in the history
… the catalog of items as json on /catalog.

Note, I'm using a global variable to allow the catalog route to see the data that comes back from the Google sheet. This will need to be changed at some point.
  • Loading branch information
Zachary Smith committed Dec 14, 2015
1 parent c82e847 commit 96781e7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
40 changes: 36 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,56 @@ 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');
});

// 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);
Expand All @@ -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;
Expand Down

0 comments on commit 96781e7

Please sign in to comment.