-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (52 loc) · 1.63 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
const fs = require('fs');
const express = require('express');
const cors = require('cors');
const { SheetGetter } = require('../SheetGetter.js');
const app = express();
const port = 3000;
app.use(cors());
// Init the SheetGetter object
const creds = JSON.parse(fs.readFileSync('sheet-getter-example-f2cc9e4562d1.json'));
const sheetId = '1mjz4A4RzXN0hHj3Ww-nUOtk-WSrsi9B5-GvA-ZEuAKA';
const mySheetGetter = new SheetGetter(sheetId, creds);
// Store the latest data fetched from the spreadsheet here
const cache = {
animalInfo: null
};
// Helper function that fetches data from a sheet
async function getAnimalInfo() {
return mySheetGetter.getSheet('Animals with fraudulent diplomas')
.then((res) => {
const rows = res.data.values;
if (rows.length) {
// Could do something fancy with rows here (e.g. filter them, parse them, w/e).
return rows;
} else {
console.log('No data found.');
return [];
}
})
.catch((err) => console.error(err));
}
// Helper function that fetches data from the sheet and drops it in the cache
const fetchLatestData = async () => {
cache.animalInfo = await getAnimalInfo();
}
// Fetch the latest sheet data on startup
fetchLatestData();
//
// Routes
//
// Respond with whatever is in the cache
app.get('/', (req, res) => res.send(cache.animalInfo));
// Update the cache with the latest
app.get('/update', async (req, res) => {
try {
await fetchLatestData();
res.send('Updated!');
} catch (e) {
console.error(e);
res.send(`${e.stack}`);
}
});
app.listen(port, () => console.log(`Example sheet getter server listening on port ${port}!`));