-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
103 lines (92 loc) · 2.42 KB
/
server.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
101
102
103
// server.js
var express = require('express');
var app = express();
var sqlite3 = require('sqlite3').verbose();
app.set('view engine', 'ejs');
app.use(express.static("public"));
let db = new sqlite3.Database('./bnc.db', (err) => {
if (err) {
console.error(err.message);
}
console.log('Connected to the database.');
});
app.get('/', function (req, res) {
db.all('SELECT * FROM CATEGORY', [], (err, rows) => {
if (err) {
throw err;
}
res.render('index', {
data: rows
});
});
})
app.get('/all', function (req, res) {
let bots = [];
let chan = [];
const text = 'Displayed below are all the bots and channels available on both the website and bot.';
db.all('SELECT * FROM BOTS ORDER BY NAME ASC', [], (err1, result1) => {
if (err1) {
throw err1;
}
bots = result1;
console.log('Bots obtained.');
db.all('SELECT * FROM ANNOUNCEMENT ORDER BY NAME ASC', [], (err2, result2) => {
if (err2) {
throw err2;
}
chan = result2;
console.log('Channels obtained.');
res.render('all', {
toptxt: text,
bots: bots,
channels: chan
});
});
});
})
app.get('/category/:cat*', function (req, res) {
let cat = req.params.cat;
if (req.params.cat == 'Halls') {
cat = 'Halls/Residential Colleges';
}
let bots = [];
let chan = [];
const text = `Listed below are the bots and channels available in ${cat}.`;
db.all(`SELECT * FROM BOTS WHERE CATEGORY='${cat}' ORDER BY NAME ASC`, [], (err, result1) => {
if (err) {
throw err;
}
bots = result1;
console.log('Bots obtained.');
db.all(`SELECT * FROM ANNOUNCEMENT WHERE CATEGORY='${cat}' ORDER BY NAME ASC`, [], (err2, result2) => {
if (err2) {
throw err2;
}
chan = result2;
console.log('Channels obtained.');
if (bots.length + chan.length < 1) {
res.render('404');
} else {
res.render('all', {
toptxt: text,
bots: bots,
channels: chan
});
}
});
});
});
app.get('/submit', function (req, res) {
res.render('submit');
});
app.get('/about', function (req, res) {
res.render('about');
});
app.get('*', function (req, res) {
res.render('404');
});
var server = app.listen(process.env.PORT || 8086, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Server listening at http://%s:%s', host, port);
})