This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
63 lines (58 loc) · 1.8 KB
/
app.js
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
const express = require('express');
const {
v4: uuidv4
} = require('uuid');
const fileUpload = require('express-fileupload');
const pdf_table_extractor = require("pdf-table-extractor");
const fs = require('fs');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.use(fileUpload());
app.get('/', (req, res) => {
res.send({
status: true,
message: 'Hello, World!'
});
});
app.post('/upload', async (req, res) => {
if (!req.files.file) {
res.status(400).send({
status: false,
message: 'No file uploaded'
});
} else {
console.log(req.files.file);
let filename = uuidv4();
console.log(`Storing as ${filename}`);
await req.files.file.mv('./uploads/' + filename);
try {
console.log(`Parsing ${filename}`);
pdf_table_extractor('./uploads/' + filename, result => {
console.log(`Parsing result: ${JSON.stringify(result)}`);
res.send({
status: true,
message: `Parsed ${filename}`,
source: req.files.file.name,
data: result
});
console.log(`Deleting ${filename}`);
fs.unlinkSync('./uploads/' + filename);
}, error => {
console.log(error);
res.status(500).send({
status: false,
message: `Failed to parse file ${filename}`
});
});
} catch (error) {
console.log(error);
res.status(500).send({
status: false,
message: `Failed to parse file ${filename}`
});
}
}
});
app.listen(PORT, HOST);
console.log(`Listening on http://${HOST}:${PORT}`);