-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (54 loc) · 1.88 KB
/
index.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
const express = require('express');
const multer = require('multer');
var bodyParser = require('body-parser');
var node_xj = require("xls-to-json");
const app = express();
app.use(bodyParser.json());
var storage = multer.diskStorage({ //multers disk storage settings
destination: function(req, file, cb) {
cb(null, './uploads/')
},
filename: function(req, file, cb) {
var datetimestamp = Date.now();
if (file) {
cb(null, datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length - 1])
}
}
});
var upload = multer({ //multer settings
storage: storage
}).single('file');
app.get('/api/home', (req, res) => {
res.json({
status: 1,
message: 'app run successfully'
})
});
app.post('/api/uploadfile', (req, res) => {
upload(req, res, function(err) {
if (err) {
res.json({ error_code: 1, err_desc: err });
return;
}
node_xj({
input: './uploads/' + req.file.filename, // input xls
// output: './uploads/' + "output.json", // output json
// sheet: "sheetname", // specific sheetname
// rowsToSkip: 5, // number of rows to skip at the top of the sheet; defaults to 0
// allowEmptyKey: false, // avoids empty keys in the output, example: {"": "something"}; default: true
},
function(err, result) {
if (err) {
res.json({ error_code: 1, err_desc: err });
} else {
res.json({ error_code: 0, err_desc: null, result: result });
}
}
);
});
});
let server = app.listen((process.env.PORT || 3000), function() {
let host = server.address().address;
let port = server.address().port;
console.log("App listening at http://%s:%s", host, port);
})