This repository was archived by the owner on Feb 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (90 loc) · 2.97 KB
/
Copy pathindex.js
File metadata and controls
105 lines (90 loc) · 2.97 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
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
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const cors = require('cors')
const {csvFormat} = require('d3-dsv');
const {lru, getSheetInfo, getWorksheet, getWorksheetList, setCache} = require('./middleware');
const app = express();
app.use(helmet());
app.use(cors());
const config={ sitename: 'ExpreCSV (CORS enabled)' };
app.get('/', (req, res)=>{
res.json(`${config.sitename}`);
});
app.get('/data/:sheetId/:worksheetTitle.csv',
lru, getSheetInfo, getWorksheet,
function (req, res) {
if(req.cacheKey){
setCache(req.cacheKey, req.rows);
}
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', `inline;filename="${req.worksheetTitle}.csv"`)
res.send(csvFormat(req.rows));
});
app.get('/data/:sheetId/:worksheetTitle.json',
lru, getSheetInfo, getWorksheet,
function (req, res) {
if(req.cacheKey){
setCache(req.cacheKey, req.rows);
}
res.json(req.rows);
});
app.get('/data/:sheetId/dictionary/:worksheetTitle-by-:key.json',
lru, getSheetInfo, getWorksheet,
function (req, res) {
const dictionary = req.rows.reduce((acc, current)=>{
if(!acc[current[req.params.key]]){
acc[current[req.params.key]] = [];
}
acc[current[req.params.key]].push(current);
return acc;
},{});
if(req.cacheKey){
setCache(req.cacheKey, dictionary);
}
res.json(dictionary);
});
app.get('/data/:sheetId/find/:key=:value-in-:worksheetTitle.json',
lru, getSheetInfo, getWorksheet,
function (req, res) {
const found = req.rows.filter( row=>(row[req.params.key] == decodeURI(req.params.value)) );
if(req.cacheKey){
setCache(req.cacheKey, found);
}
res.json(found);
});
app.get('/data/:sheetId.json',
lru, getSheetInfo, getWorksheetList,
function (req, res) {
const host = req.get('host');
const protocol = req.protocol;
const worksheets = req.worksheets.map(title => ({
title,
json: `${protocol}://${host}/data/${req.sheetId}/${title}.json`,
csv: `${protocol}://${host}/data/${req.sheetId}/${title}.csv`,
dict: `${protocol}://${host}/data/${req.sheetId}/dictionary/${title}-by-[column].json`,
find: `${protocol}://${host}/data/${req.sheetId}/find/[column]-[value]-in-${title}.json`
}));
if(req.cacheKey){
setCache(req.cacheKey, { worksheets });
}
res.json({ worksheets });
});
app.use(function(err, req, res, next) {
console.error('ERROR handler', err.message); // Log error message in our server's console
res.status(500)
.json({
status: 500,
err: err.message
}); // All HTTP requests must have a response, so let's send back an error with its status code and message
});
app.use(function (req, res, next) {
res.status(404).json({
status:404,
message:`Sorry! Couldn\'t find ${req.originalUrl}`
});
});
const port = process.env.PORT || 3000;
app.listen(port, function(){
console.log(`ExpreCSV is running on ${port}`);
});