-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
140 lines (119 loc) · 4.07 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Created by susil panda on 10/23/2016.
*/
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('swaggykuteer', [ 'swaggykuteer']);
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var excelParser = require('excel-parser');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(express.static(_dirname = '/public'));
app.use(bodyParser.json());
app.get('/products', function(req, res){
console.log("I got Get All Request");
db.swaggykuteer.find(function(err,docs){
console.log(docs);
res.json(docs);
});
});
app.post('/create', function(req, res){
console.log("I got a create Request");
console.log(req.body);
db.swaggykuteer.insert(req.body, function(err, doc){
res.json(doc);
console.log(err);
});
});
app.get('/getProduct/:id', function(req, res){
console.log("got a get Request");
var id = req.params.id;
db.swaggykuteer.findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
res.json(doc);
console.log(err);
});
});
app.put('/update/:id', function (req, res) {
console.log("update request received");
var id = req.params.id;
console.log(id);
db.swaggykuteer.findAndModify({
query:{_id: mongojs.ObjectId(id)},
update:{$set:{name:req.body.name , experience:req.body.experience, currentproject:req.body.currentproject, emailid:req.body.emailid, mobilenumber:req.body.mobilenumber,
profile:req.body.profile, role:req.body.role, awards:req.body.awards, certification:req.body.certification, testframework:req.body.testframework }},new : true },function(response){
console.log("updated successfully");
console.log(response);
res.json(response);
});
});
function convertToJSON(array) {
var first = array[0].join()
var headers = first.split(',');
var jsonData = [];
for ( var i = 1, length = array.length; i < length; i++ )
{
var myRow = array[i].join();
var row = myRow.split(',');
var data = {};
for ( var x = 0; x < row.length; x++ )
{
data[headers[x]] = row[x];
}
jsonData.push(data);
}
return jsonData;
};
app.post('/uploadData', function (req, res) {
/*function parseAndSaveInDB(req, res) {
console.log("Inside uploadData");
/!*excelParser.worksheets({
inFile: 'data/productData.xlsx'
}, function (err, worksheets) {
if (err) console.error(err);
console.log(worksheets);
});*!/
excelParser.parse({
inFile: 'data/master.xls',
worksheet: 1,
skipEmpty: true,
searchFor: {
term: ['my search term'],
type: 'loose'
}
},function(err, records){
if(err) console.error(err);
console.log(records);
});
//console.log(jsonDataArray(data));
//console.log(JSON.stringify(convertToJSON(data)));
console.log("End of Parsing");
};*/
var exceltojson = require("xlsx-to-json-lc");
exceltojson({
input: "data/productData.xlsx",
output: null, //"if you want output to be stored in a file",
// sheet: "sheetname", // specific sheetname inside excel file (if you have multiple sheets)
lowerCaseHeaders: true //to convert all excel headers to lowr case in json
}, function (err, result) {
if (err) {
console.error(err);
} else {
console.log("Excel parsed");
//console.log(result);
//result will contain the overted json data
//save result to DB
db.swaggykuteer.insert(result, function(err, doc){
res.json(doc);
console.log(err);
});
}
});
});
app.listen(3033);