-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (46 loc) · 1.69 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
var express = require('express');
var app = express();
var path = require('path');
var multer = require('multer');
var upload = multer();
var fs = require('fs');
var csv = require('fast-csv');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/color25';
var upload = multer({ dest: 'uploads/' })
app.get('/', function(req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
app.post('/upload', upload.single('file'),function(req, res, next) {
if (req.file) {
var dataResult = [];
csv
.fromPath(req.file.path, {headers : true, objectMode: true})
.on('data', function(data){
dataResult.push(data);
})
.on('end', function(){
var insertDocument = function(db, callback) {
db.collection('guests').insertMany( dataResult , function(err, result) {
assert.equal(err, null);
console.log("Inserted documents into the guests collection.");
callback(result);
});
};
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
db.close();
});
});
res.json(dataResult);
});
} else {
res.send('Error!');
}
});
var port = process.env.PORT || 8080;
app.listen(port);
console.log('Listen: ' + port);