-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (91 loc) · 3.12 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Require and create an instance of express
var express = require('express');
var app = express();
// Require Vichaar schema
var Vichaar = require('./schemas/Vichaar.js');
// Assign port number
var PORT = process.env.PORT || 3000;
// GET method route for requests to /random
app.get('/random', (req, res) => {
// Initialise n
var n = req.query.n ? Number(req.query.n) : 1;
// If vishay parameter specified
if (req.query.vishay) {
// Assign vishay
var vishay = req.query.vishay.replace(/^\w/, (char) => { return char.toUpperCase(); });
// Count the number of relevant documents
Vichaar.count({ vishay: vishay })
.exec((err, count) => {
if (err) {
// Log error, if any
console.log(err);
} else {
// Find a random number in [1, count]
var random = Math.floor(Math.random() * (count - 1)) + 1;
// Find relevant document(s) after skipping 'random' documents
Vichaar.find({ vishay: vishay }, { _id: 0, __v: 0 })
.skip(random)
.limit(n)
.exec((err, vichaar) => {
if (err) {
// Log error, if any
console.log(err);
} else {
// Send response as json
res.json(vichaar);
}
});
}
});
} else {
// Count total number of documents
Vichaar.count((err, count) => {
if (err) {
// Log error, if any
console.log(err);
} else {
// Find random number(s) in [1, count]
var randomArray = [];
for (var i = 0; i < n; i++) {
randomArray.push(Math.floor(Math.random() * (count - 1)) + 1);
}
// Find the relevant document(s)
Vichaar.find({ sankhya: { $in: randomArray } }, { _id: 0, __v: 0 })
.limit(n)
.exec((err, vichaar) => {
if (err) {
// Log error, if any
console.log(err);
} else {
// Send response as json
res.json(vichaar);
}
});
}
});
}
});
// GET method for requests to /
app.get('/', (req, res) => {
// If sankhya parameter specified
if (req.query.sankhya) {
// Find the relevant document
Vichaar.find({ sankhya: req.query.sankhya }, { _id: 0, __v: 0 })
.exec((err, vichaar) => {
if (err) {
// Log error, if any
console.log(err);
} else {
// Send response as json
res.json(vichaar);
}
});
} else {
// Redirect to repo page
res.redirect('https://github.com/abhaykv04/vichaar');
}
});
// Listen on PORT
app.listen(PORT, () => {
console.log('(vichaar): Listening on port', PORT);
});