-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·51 lines (42 loc) · 1.84 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
'use strict';
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs-extra');
var _ = require('underscore');
// ----------------------------------------------------------------------------
var app = express();
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({extended: true}));
// ----------------------------------------------------------------------------
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// ----------------------------------------------------------------------------
app.all('/', function (req, res) {
var result;
if (req.body && req.body.login && req.body.password) {
var list = fs.readJsonSync('data/people.json');
var login = req.body.login.trim();
var password = req.body.password.trim();
if (list[login] && list[login].password === password) {
if (list[login].done === false) {
var random = _.chain(list).omit(login).pairs().reject(function (item) {
return item[1].booked === true;
}).shuffle().first().value();
if (random) {
list[login].done = true;
list[random[0]].booked = true;
fs.writeJsonSync('data/people.json', list, {spaces: 2});
result = 'Тобі випало: <strong>' + random[1].name + '</strong>';
}
} else {
result = 'Ти вже знаєш, що тобі випало :)';
}
}
}
res.render('pages/index', {result: result});
});
// ----------------------------------------------------------------------------
app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
});