-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (60 loc) · 1.67 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
const express = require('express');
const checkList = require('./checkList');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
// Handlebars helpers
var hbs = exphbs.create({
// Specify helpers which are only registered on this instance.
helpers: {
ifCond: function (v1, v2, options) {
if (v1 || v2) {
return options.fn(this);
}
return options.inverse(this);
},
varyok: function (v, options) {
if (v) {
return 'VAR';
}
return 'YOK';
},
},
defaultLayout: 'main'
});
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// Main route
app.get('/', (req, res, next) => {
res.render('index');
return;
});
app.get('/help', (req, res, next) => {
res.render('help');
return;
});
// Result route
app.post('/checkList', (req, res, next) => {
// CSV to json object
const list = req.body.txtListe.split('\n');
const listFormated = [];
const headers = list[0].split(';');
for (var i = 1; i < list.length; i++) {
const data = list[i].split(';');
const obj = {};
for (var j = 0; j < data.length; j++) {
obj[headers[j].trim()] = data[j].trim();
}
listFormated.push(obj);
}
JSON.stringify(listFormated);
// Get the results
return checkList(listFormated, (err, result) => {
res.render('result', { result });
return;
});
});
app.listen(3000, () => {
console.log('Example app listening on port 80!');
});