-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (113 loc) · 3.62 KB
/
main.js
File metadata and controls
129 lines (113 loc) · 3.62 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
var fs = require('fs');
const jsonparse = require('csv-parse')
const JsonTocsv = require('csv-parser')
const JsonToForm = require('csv-parser')
const mysql = require('mysql');
const express = require('express');
var jsonexport = require('jsonexport');
const path=require('path');
var formidable = require('formidable');
var bodyParser = require('body-parser');
const cors = require('cors');
const app =express();
app.set("views",path.join(__dirname,"views"))
app.set("view engine","pug")
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors());
app.options('*', cors());
//**json/csv API WORKING */
/* const db =mysql.createConnection({
connectionLimit : 100,
host: 'localhost',
user :'root',
password:'',
database:'json_parsing'
});
db.connect((err)=>{
if(err){throw err; }
console.log('connection established..'); });
app.listen('3000',()=>{console.log('server started on port 3000');});
var uiForm = fs.readFileSync(".//views//upload.html");
app.get("/",function (req,res){
res.writeHead(200);
res.write(uiForm);
res.end();
})
app.post("/",function (req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.ext.path;
let jsonArr=[];
fs.readFile(oldpath, 'utf8', function(err, contents) {
const filedata = JSON.parse(contents)
var results=[]
var nodeValues=Object.values(filedata);
results.push(nodeValues);
jsonexport(results,function(err, csvout){
if(err) return console.log(err);
fs.writeFile(".//files/nour.csv", csvout , function(err, result) {
if(err)
console.log('error', err);
fs.createReadStream(".//files/nour.csv")
.pipe( JsonToForm({delimiter:','}))
.on("data", function (jsonRow) {
jsonArr.push(jsonRow);
}).on("end", function () {
res.render('jsonForm', { load: contents, csvlook:jsonArr })
});
})
}) */
//**********get headers and store into DB */
/* const JsonHeaders= ()=>{
const arrayset = [];
fs.createReadStream('.//files/nour.csv')
.pipe(JsonTocsv())
.on('headers', (headers) => {
arrayset.push(headers)
let i = 0;
let text = headers;
for (i ; i<text.length ;i++){
db.query('ALTER TABLE json_table ADD '+text[i]+' varchar(255)',(error, response) => {
console.log(error || response);
});
}
})
}
JsonHeaders()
*/
//**********get columns and store into DB */
/*
const jsonColumns= ()=>{
let jsondata = [];
fs.createReadStream('.\\files\\nour.csv')
.pipe(
jsonparse({delimiter:','}))
.on("data", function (dataRow) {
jsondata.push(dataRow);
})
.on("end", function () {
jsondata.shift();
let sql = 'ALTER TABLE json_table DROP id';
db.query(sql,(error, response) => {
console.log(error || response);
});
let query = 'INSERT INTO json_table VALUES ?';
db.query(query, [jsondata], (error, response) => {
console.log(error || response);
});
});
}
jsonColumns()
})
})
}) */
let counter = 0
app.get("/hat",function (req, res) {
console.log(++counter)
}
)
const listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port)
})