Skip to content

Instantly share code, notes, and snippets.

@teezzan
Created January 7, 2021 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teezzan/821a70de70b9f48b9f65863602705042 to your computer and use it in GitHub Desktop.
Save teezzan/821a70de70b9f48b9f65863602705042 to your computer and use it in GitHub Desktop.
Test Script
const express = require("express");
const fs = require('fs')
const app = express();
const axios = require("axios");
const bodyParser = require('body-parser');
const _ = require("lodash");
const { v4: uuidv4 } = require('uuid');
global.window = {}
const { jsPDF } = require('jspdf/dist/jspdf.node')
const { applyPlugin } = require('./node_modules/jspdf-autotable/dist/jspdf.plugin.autotable');
const FormData = require('form-data')
applyPlugin(jsPDF);
app.use(bodyParser.json());
const pdfy = async (head, body) => {
let new_body = body.map((el) => Object.values(el));
const doc = new jsPDF()
doc.autoTable({
head: [head],
body: new_body,
})
const data = doc.output()
fs.writeFileSync('./document.pdf', data, 'binary');
// const form_data = new FormData();
// form_data.append("file", newFile);
console.log('here too');
// return axios({
// method: "post",
// url: "https://file.io/?expires=1d",
// headers: {
// "Content-Type": "multipart/form-data"
// },
// data: form_data
// });
const response = await axios({
method: "post",
url: "https://file.io",
data: Buffer.from(data),
data: Buffer.from("bufStr hellllo", 'utf8'),
headers: { "Content-Type": "application/text" }
});
console.log(response.data)
}
app.post("/", async (req, resp) => {
let payload = req.body;
let csv;
try {
if (payload.csv) {
if (payload.csv.url == null ||
payload.csv.url == "" ||
payload.csv.select_fields == undefined ||
payload.csv.select_fields.length == 0)
resp.status(442).json({ message: "Wrong Format" })
let result = await axios.get(payload.csv.url);
if (result.status == 200) {
let csv_data = result.data
console.log("here")
let output = csv_data.map((el) => _.pick(el, payload.csv.select_fields))
let random = uuidv4();
random = random.split('-').join('').toUpperCase();
if (payload.pdf) {
let a = await pdfy(payload.csv.select_fields, output);
console.log(a)
}
resp.status(200).json({ random, json: output })
}
}
else {
resp.status(442).json({ message: "csv required" })
}
}
catch (err) {
console.log(err)
resp.status(500).json({
message: "Internal server Error"
})
}
});
const listener = app.listen(process.env.PORT || 3001, () => {
console.log("Your app is listening on port " + listener.address().port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment