-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (78 loc) · 2.37 KB
/
Copy pathapp.js
File metadata and controls
96 lines (78 loc) · 2.37 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
const express = require('express');
const csv = require('fast-csv');
const path = require('path');
const app = express();
const fs = require('fs');
const port = 3000;
app.use(express.json());
var file_data = fs.readFileSync('./public/company.txt', 'utf8');
var data = file_data.split('\n');
app.get('/getCompany', (req, res) => {
res.status(200).json(data);
});
function findCsvFile(directoryPath, fileName) {
const files = fs.readdirSync(directoryPath);
for (const file of files) {
const filePath = path.join(directoryPath, file);
if (path.extname(filePath) === '.csv' && file === fileName) {
return filePath;
}
}
return null;
}
var readCsvFile = async (filePath) => {
return new Promise((resolve, reject) => {
var results = [];
fs.createReadStream(filePath)
.pipe(csv.parse({ headers: true }))
.on('error', error => {
console.error(error);
reject(error);
})
.on('data', row => {
results.push(row);
})
.on('end', rowCount => {
if (results.length > 0) {
resolve(results);
} else {
console.warn('CSV file is empty');
reject('CSV file is empty');
}
});
});
}
app.post('/getCompanyInfo', async (req, res) => {
console.log(req.body);
const { company } = req.body;
const directoryPath = './data';
const fileName = company + '_alltime.csv';
const filePath = findCsvFile(directoryPath, fileName);
if (filePath) {
var data = await readCsvFile(filePath);
if (data) {
res.status(200).json(data);
} else {
console.warn('CSV file is empty');
res.status(500).send('Something went wrong!');
}
} else {
res.status(404).send('Resource not found');
}
});
app.get('/', (req, res) => {
res.redirect('/tc');
});
app.get('/tc', (req, res) => {
res.sendFile(path.join(process.cwd(), 'public', 'tc.html'));
});
app.use((err, req, res, next) => {
console.error("Req" + req);
console.error(err.stack);
res.status(500).send('Something went wrong!');
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
// Export for server build
module.exports = app;