-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
147 lines (120 loc) · 3.6 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
141
142
143
144
145
146
147
var express = require('express');
var app = express();
//var mongojs = require('mongojs'); //When using local MongoDB
//var db = mongojs('mytask',['marklist']);
var mLab = require('mongolab-data-api')('ff6vl0JOX9RkcjG38JqRKx5uIk6WUfWx');
var bodyParser =require('body-parser');
var fs = require('fs');
var multer = require('multer')
var upload = multer({ dest: 'public/uploads/' })
var csvjson = require('csvjson');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.get('/mytask', function (req, res){
console.log("I receive a GET request")
//Local DB
/*db.marklist.find().sort({Percentile : -1}, function (err, docs){
//console.log(docs);
res.json(docs);
});*/
//MLAB
var options = {
database: 'maz',
collectionName: 'mark'
//query: '{ "key": "value" }'
};
mLab.listDocuments(options,function (err, data) {
//console.log(data); //=> [ { _id: 1234, ... } ]
res.json(data);
});
});
//listdb
mLab.listDatabases(function (err, data) {
if (err) { console.log(err); }
else {
console.log("database",data); // => [db1, db2, db3, ...]
}
});
//list collections
mLab.listCollections('maz', function (err, collections) {
console.log("collections",collections); // => [coll1, coll2, ...]
});
//********************************* INSERT SINGLE RECORD********************************
app.post('/mytask',function(req, res){
//Local DB
//console.log(req.body);
/*db.marklist.insert(req.body, function(err, doc){
res.json(doc);
});*/
//MLAB
var options = {
database: 'maz',
collectionName: 'mark',
documents: req.body
};
mLab.insertDocuments(options, function(err, doc){
res.json(doc);
});
});
//***************************************** DELETION*****************************************
app.delete('/mytask/:id', function (req, res){
var id = req.params.id;
//console.log(id);
/*db.marklist.remove({_id:mongojs.ObjectId(id)}, function (err, doc){
res.json(doc);
})*/
var options = {
database: 'maz',
collectionName: 'mark',
id: id
};
mLab.deleteDocument(options, function (err, doc){
res.json(doc);
});
});
/* app.get('/fileUpload', function (req, res){
console.log("I receive a GET request")
var options = {
database: 'maz',
collectionName: 'mark'
};
res.redirect('/mytask');
}); */
//*************************************** UPLOAD **********************************************
app.post('/fileUpload', upload.single('test'), function(req, res,next) {
console.log("request file",req.file);
var tmp_path = req.file.path;
/** The original name of the uploaded file
stored in the variable "originalname". **/
var target_path = 'public/uploads/' + req.file.originalname;
/** A better way to copy the uploaded file. **/
var src = fs.createReadStream(tmp_path);
var dest = fs.createWriteStream(target_path);
src.pipe(dest);
src.on('end', function() {
var data = fs.readFileSync(target_path, { encoding : 'utf8'});
//var options = { delimiter : ','};
var csvjsonre=csvjson.toObject(data); //Convert CSV to JSON
//console.log(target_path);
//Local DB
/*db.marklist.insert(csvjsonre, function(err, doc){
res.json(doc);
console.log('complete');
});*/
//MLAB
var options = {
database: 'maz',
collectionName: 'mark',
documents: csvjsonre
};
mLab.insertDocuments(options, function (err, docs) {
console.log("complete",docs);
res.redirect('/');
//res.json(docs.n+" documents inserted succesfully");
});
});
src.on('error', function(err) { console.log('error'); });
});
var server = app.listen((process.env.PORT || 3000) , function(){
console.log("server running");
})