first commit
This commit is contained in:
commit
e40b2117b5
21 changed files with 17956 additions and 0 deletions
15
backend/.eslintrc.js
Executable file
15
backend/.eslintrc.js
Executable file
|
@ -0,0 +1,15 @@
|
|||
module.exports = {
|
||||
env: {
|
||||
browser: true,
|
||||
commonjs: true,
|
||||
es2021: true,
|
||||
},
|
||||
extends: [
|
||||
'airbnb-base',
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 12,
|
||||
},
|
||||
rules: {
|
||||
},
|
||||
};
|
4
backend/.gitignore
vendored
Executable file
4
backend/.gitignore
vendored
Executable file
|
@ -0,0 +1,4 @@
|
|||
node_modules
|
||||
!.gitkeep
|
||||
csv/*
|
||||
public/*
|
119
backend/index.js
Executable file
119
backend/index.js
Executable file
|
@ -0,0 +1,119 @@
|
|||
const path = "./csv";
|
||||
const data = {};
|
||||
const column = {};
|
||||
let files = [];
|
||||
|
||||
const fs = require("fs");
|
||||
const papa = require("papaparse");
|
||||
const chokidar = require("chokidar");
|
||||
|
||||
chokidar.watch(path).on("all", () => {
|
||||
console.log("File changed. Reloading ...");
|
||||
files = fs.readdirSync(`${path}`).filter((file) => file.endsWith(".csv"));
|
||||
for (const file of files) {
|
||||
const csv = fs.readFileSync(`${path}/${file}`, {
|
||||
encoding: "utf8",
|
||||
});
|
||||
const parsed = papa.parse(csv, {
|
||||
skipEmptyLines: true,
|
||||
}).data;
|
||||
|
||||
const header = parsed[0];
|
||||
column[file] = [];
|
||||
for (const h of header) {
|
||||
column[file].push({
|
||||
key: h,
|
||||
text: h,
|
||||
sorting: "",
|
||||
});
|
||||
}
|
||||
|
||||
const converted = [];
|
||||
for (let j = 1; j < parsed.length; ++j) {
|
||||
const d = parsed[j];
|
||||
const e = {};
|
||||
for (let i = 0; i < d.length; ++i) {
|
||||
e[header[i]] = d[i];
|
||||
}
|
||||
converted.push(e);
|
||||
}
|
||||
data[file] = converted;
|
||||
}
|
||||
});
|
||||
|
||||
const express = require("express");
|
||||
|
||||
const app = express();
|
||||
|
||||
const cors = require("cors");
|
||||
|
||||
app.use(cors());
|
||||
|
||||
const port = 3000;
|
||||
|
||||
app.get("/files", (_, res) => {
|
||||
res.send(files);
|
||||
});
|
||||
|
||||
function compare(a, b) {
|
||||
const m = a - b;
|
||||
if (isNaN(m)) {
|
||||
if (a > b) {
|
||||
return 1;
|
||||
}
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
app.get("/csv/:file", (req, res) => {
|
||||
const {
|
||||
file
|
||||
} = req.params;
|
||||
const raw = data[file];
|
||||
const col = column[file];
|
||||
let processed = raw;
|
||||
let query_key = "";
|
||||
let key = "";
|
||||
|
||||
const c = col.find((ele) => {
|
||||
key = ele.key;
|
||||
query_key = `sort_${key}`;
|
||||
return query_key in req.query;
|
||||
});
|
||||
if (!(c === "undefined")) {
|
||||
processed = [...raw];
|
||||
if (req.query[query_key] == "ASC") {
|
||||
processed.sort((a, b) => compare(a[key], b[key]));
|
||||
} else {
|
||||
processed.sort((a, b) => compare(b[key], a[key]));
|
||||
}
|
||||
}
|
||||
|
||||
if ("pSize" in req.query && "cPage" in req.query) {
|
||||
const size = req.query.pSize;
|
||||
const page = req.query.cPage;
|
||||
processed = processed.slice((page - 1) * size, page * size);
|
||||
}
|
||||
|
||||
res.send({
|
||||
totals: raw.length,
|
||||
data: processed,
|
||||
});
|
||||
});
|
||||
|
||||
app.use(express.static("public"));
|
||||
|
||||
app.get("/column/:file", (req, res) => {
|
||||
const {
|
||||
file
|
||||
} = req.params;
|
||||
res.send(column[file]);
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Listening at http://localhost:${port}`);
|
||||
});
|
2121
backend/package-lock.json
generated
Executable file
2121
backend/package-lock.json
generated
Executable file
File diff suppressed because it is too large
Load diff
22
backend/package.json
Executable file
22
backend/package.json
Executable file
|
@ -0,0 +1,22 @@
|
|||
{
|
||||
"name": "backend",
|
||||
"version": "0.1.0",
|
||||
"description": "backed of project csv2html",
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"chokidar": "^3.5.2",
|
||||
"express": "^4.17.1",
|
||||
"papaparse": "^5.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"eslint": "^7.30.0",
|
||||
"eslint-config-airbnb-base": "^14.2.1",
|
||||
"eslint-plugin-import": "^2.23.4"
|
||||
},
|
||||
"scripts": {
|
||||
"serve": "node index.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC"
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue