-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
49 lines (40 loc) · 1.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
var express = require('express');
var app = express();
app.set('views', 'views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.get('/', function(req, res){
//set default variables
var totalStudents = 80,
pageSize = 8,
pageCount = 80/8,
currentPage = 1,
students = [],
studentsArrays = [],
studentsList = [];
//genreate list of students
for (var i = 1; i < totalStudents; i++) {
students.push({name: 'Student Number ' + i});
}
//split list into groups
while (students.length > 0) {
studentsArrays.push(students.splice(0, pageSize));
}
//set current page if specifed as get variable (eg: /?page=2)
if (typeof req.query.page !== 'undefined') {
currentPage = +req.query.page;
}
//show list of students from group
studentsList = studentsArrays[+currentPage - 1];
//render index.ejs view file
res.render('index', {
students: studentsList,
pageSize: pageSize,
totalStudents: totalStudents,
pageCount: pageCount,
currentPage: currentPage
});
});
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});