-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
106 lines (79 loc) · 2.86 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require('express');
const bodyParser = require("body-parser");
var upload = require('express-fileupload');
var path = require('path');
var fs = require('fs');
var CSVToJSON = require("csvtojson");
const FileSystem = require("fs");
const app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
extend_csv=".csv";
extend_json=".json";
app.use(express.json());
app.use(express.static("public"));
app.use(upload());
// set the view engine to ejs
app.get("/",function(req,res){
res.sendFile(__dirname+ "/index.html");
});
var down_name;
app.post("/upload",function(req,res){
console.log("hello");
console.log(req.files);
if(req.files.upfile){
var file = req.files.upfile,
name = file.name,
type = file.mimetype;
//File where .csv will be downloaded
uploadpath = __dirname + '/uploads/' + name;
//Name of the file --ex test,example
const First_name = name.split('.')[0];
//Name to download the file
down_name = First_name;
file.mv(uploadpath,function(err){
if(err){
res.sendFile(__dirname+ "/public/404.html");
}
else
{
//Path of the downloaded or uploaded file
var initialPath = path.join(__dirname, `./uploads/${First_name}${extend_csv}`);
//Path where the converted json will be placed/uploaded
var upload_path = path.join(__dirname, `./uploads/${First_name}${extend_json}`);
CSVToJSON().fromFile(initialPath).then(source => {
console.log(source);
FileSystem.writeFile(upload_path, JSON.stringify(source, null, 4), (err) => {
if (err) {
res.sendFile(__dirname+ "/public/404.html");
}
console.log("Downloaded");
res.download(__dirname +`/uploads/${down_name}${extend_json}`,`${down_name}${extend_json}`,(err) =>{
if(err){
res.sendFile(__dirname+ "/public/404.html");
}else{
//Delete the files from directory after the use
console.log('Files deleted');
const delete_path_csv = process.cwd() + `/uploads/${down_name}${extend_csv}`;
const delete_path_json = process.cwd() + `/uploads/${down_name}${extend_json}`;
try {
fs.unlinkSync(delete_path_csv)
fs.unlinkSync(delete_path_json)
//file removed
} catch(err) {
res.sendFile(__dirname+ "/public/404.html");
}
}
});
});
})
}});
}
else{
res.sendFile(__dirname+ "/public/404.html");
}
});
app.listen(3000,() => {
console.log("Server Started at port 3000...");
});