Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
98 lines (80 loc) · 2.52 KB
/
Copy pathserver.js
File metadata and controls
98 lines (80 loc) · 2.52 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
// Execute before running node server : fnm env --use-on-cd | Out-String | Invoke-Expression
const express = require('express');
const fs = require('fs');
const Papa = require('papaparse');
const path = require('path');
const app = express();
const PORT = 3000;
// Merge csv files:
const csv = require('csv-parser');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvDir = 'CSV';
const csvFiles = fs.readdirSync(csvDir)
.filter(file => path.extname(file).toLowerCase() === '.csv')
.map(file => path.join(csvDir, file));
const outputCSV = 'mergedCSV.csv';
const results = [];
const readCsvFile = (filePath) => {
return new Promise((resolve, reject) => {
const rows = [];
fs.createReadStream(path.resolve(filePath)) // 경로를 절대 경로로 변환
.pipe(csv())
.on('data', (data) => rows.push(data))
.on('end', () => {
resolve(rows);
})
.on('error', (error) => {
reject(error);
});
});
};
const writeCsvFile = (filePath, data) => {
const csvWriter = createCsvWriter({
path: filePath,
header: Object.keys(data[0]).map((key) => ({ id: key, title: key })),
});
return csvWriter.writeRecords(data);
};
const mergeCsvFiles = async () => {
try {
for (const file of csvFiles) {
const rows = await readCsvFile(path.resolve(file));
results.push(...rows);
}
if (results.length > 0) {
await writeCsvFile(outputCSV, results);
console.log('Merged successfully.');
} else {
console.log('No data found');
}
} catch (error) {
console.error('Error merging: ', error);
}
};
mergeCsvFiles();
app.use(express.static('public'));
app.use('/user-stat', express.static('user-stat'));
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.get('/user-stat', (req, res) => {
res.sendFile(path.join(__dirname, 'user-stat', 'index.html'));
});
app.get('/players', (req, res) => {
fs.readFile('mergedCSV.csv', 'utf8', (err, data) => {
if (err) {
res.status(500).send('Error reading the CSV file');
return;
}
Papa.parse(data, {
header: true,
complete: function(results) {
const sortedData = results.data.sort((a, b) => b.asset - a.asset);
res.json(sortedData);
}
})
})
})
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
})