-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 2.37 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express')
const mongoose = require('mongoose')
const multer = require('multer')
const csvtojson = require('csvtojson')
const Student = require('./studentSchema');
const app = express()
console.log("commit from vasanti");
console.log("console from master");
console.log("console from other");
mongoose.connect('mongodb://localhost:27017/MongoExcelDemo').then(() => {
console.log('database connected')
});
app.use(express.static('public')) // static folder
app.set('view engine','ejs') // set the template engine
// multer disk storage
var excelStorage = multer.diskStorage({
destination:(req,file,cb)=>{
cb(null,'./public/excelUploads'); // file added to the public folder of the root directory
},
filename:(req,file,cb)=>{
cb(null,file.originalname);
}
});
var excelUploads = multer({storage:excelStorage});
app.get('/',(req,res) => {
res.render('index.ejs');
console.log("demo");
console.log("demo vasanti branch");
res.render('index.ejs');
console.log("changed after pull");
})
// upload excel file and import in mongodb
app.post('/uploadExcelFile', excelUploads.single("uploadfile"), (req, res) =>{
importFile('./public' + '/excelUploads/' + req.file.filename);
function importFile(filePath){
// Read Excel File to Json Data
var arrayToInsert = [];
csvtojson().fromFile(filePath).then(source => {
// Fetching the all data from each row
for (var i = 0; i < source.length; i++) {
console.log(source[i]["name"])
var singleRow = {
name: source[i]["name"],
email: source[i]["email"],
standard: source[i]["standard"],
};
arrayToInsert.push(singleRow);
}
//inserting into the table student
Student.insertMany(arrayToInsert, (err, result) => {
if (err) console.log(err);
if(result){
console.log("File imported successfully.");
res.redirect('/')
}
});
});
}
})
app.listen(3000, () => {
console.log('server started at port 3000')
})