-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (54 loc) · 1.87 KB
/
index.js
File metadata and controls
63 lines (54 loc) · 1.87 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const path = require('path');
const parser = require('simple-excel-to-json')
const multer = require('multer');
const os = require('os');
const app = express();
//middlewares
const upload = multer({ dest: os.tmpdir() });
app.use(bodyParser.json({limit:'50mb'}));
app.use(bodyParser.urlencoded({
limit: '50mb',
extended: true,
parameterLimit:50000
}));
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
if (req.method === 'OPTIONS') {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
res.status(200);
}
next();
});
//wild card route
app.get('*', (req, res) => {
res.json({
message:"The endpoint you are trying to access is not avaliable !"
}).status(404);
});
app.post('/api/v2/xlsx-to-json',upload.single('file'),async(req,res,next)=>{
try {
const file = req.file;
if(file.mimetype === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"){
var doc = parser.parseXls2Json(file.path);
if (doc) {
return res.json({"message":doc,"status":"200"});
}
else{
return res.json({"message":"Something Went Wrong ! Try Contacting App Administrator","status":"404"});
}
}
else{
return res.json({"message":"Please upload a file with .xlsx extension","status":"403"});
}
} catch (error) {
next(error);
}
});
const port = process.env.PORT || 8080;
app.listen(port, () => { console.log(`Server has been started on ${port} 🔥`) });