-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
91 lines (82 loc) · 3.04 KB
/
app.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
const express = require('express');
const path = require('path');
const upload = require('express-fileupload');
const bodyParser = require('body-parser');
const app = express();
const userRoutes = require('./routes/userRoutes');
const nanRoutes = require('./routes/nanRoutes');
const mongoose = require('mongoose');
const Lasfile = require('./models/lasFile');
// for parsing las files
const Lasjs = require('las-js');
app.use(express.static('public'));
app.use(userRoutes);
app.use(upload());
//app.use(nanRoutes);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: false}));
// Connect to database
const MONGODB_URI = 'mongodb+srv://islam:ozyKGjXmWIHhOeMM@cluster0-jrlnr.mongodb.net/logReader';
mongoose.connect(MONGODB_URI, { useNewUrlParser: true }).then(()=> {
console.log('connected to database');
app.listen(3000, ()=> {
console.log('Listen to port: 3000');
});
})
.catch(()=> {
console.log('cant connect to database');
});
// Main function
app.post('/upload-file', (req, res, next) => {
if(req.files) {
var file = req.files.filename,
filename = file.md5 + '-' + Date.now();
file.mv('./upload/' + filename , (err) => {
if (err) {
res.send('err');
}
else {
async function read() {
try {
const myLas = new Lasjs('./upload/' + filename);
const data = await myLas.dataStripped;
const headers = await myLas.header;
const headerAndDescr = await myLas.headerAndDescr;
const finalData = {};
for (var i=0; i < headers.length; i++){
var rowArray = [];
data.forEach(row => {
rowArray.push(row[i]);
});
finalData[headers[i]] = rowArray;
}
const myData = new Lasfile({
fileName: filename,
headers: headers,
headersDesc: headerAndDescr,
finalData: finalData
});
myData.save()
.then(results=>{
headers.shift();
res.render('logs', {
pageTitle: 'All logs',
headers: headers,
headersDesc: headerAndDescr,
fileName: filename,
id: results._id
});
})
.catch(err => {
console.log(err);
});
}
catch (err) {
console.log(err);
}
}
read();
}
});
}
});