-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (41 loc) · 1.4 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
var express = require('express');
var exphbs = require('express-handlebars');
var bodyParser = require('body-parser');
var _ = require('lodash');
var app = express();
app.set('port', (process.env.PORT || 5000));
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function(request, response){
response.render('index');
});
app.post('/randomize', function(request, response){
var itemString = request.body.randomItems;
var items = [];
itemString.split('\n').forEach( function(stringPart) {
stringPart = _.trim(stringPart);
if (stringPart !== '') {
items.push(stringPart);
}
});
var length = items.length;
var randomNumber = parseInt(Math.random() * length);
var pickedItem = items[randomNumber];
console.log('remove',request.body.remove);
// if we should remove the checked item
if (request.body.remove) {
var beforeItems = items.slice(0, randomNumber);
var afterItems = items.slice(randomNumber + 1);
items = beforeItems.concat(afterItems);
}
itemString = items.join('\n');
response.render('index',
{itemString: itemString, pickedItem: pickedItem});
});
app.get('/randomize', function(request, response){
response.redirect('/');
});
app.listen(app.get('port'), function(){
console.log('listening on: ', app.get('port'));
});