<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[DigitalAholic - Medium]]></title>
        <description><![CDATA[DigitalAholic is a web and application development blog covering development, programming and digital tech. - Medium]]></description>
        <link>https://medium.com/digitalaholic?source=rss----1a85c3e658b2---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>DigitalAholic - Medium</title>
            <link>https://medium.com/digitalaholic?source=rss----1a85c3e658b2---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 10:44:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/digitalaholic" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simple NodeJs Pagination]]></title>
            <link>https://medium.com/digitalaholic/simple-nodejs-pagination-a2d787b77581?source=rss----1a85c3e658b2---4</link>
            <guid isPermaLink="false">https://medium.com/p/a2d787b77581</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Perminder Klair]]></dc:creator>
            <pubDate>Tue, 11 Oct 2016 16:34:01 GMT</pubDate>
            <atom:updated>2016-10-11T16:36:11.358Z</atom:updated>
            <content:encoded><![CDATA[<p>This is really simple example tutorial about how to crate pagination in NodeJs Application (it also covers ExpressJs Framework).</p><p>We will be using EJS templating system for demo purpose.</p><p>### Setup Application<br>To, start the tutorial let’s setup a new simple ExpressJs application by creating a file named `package.json` with following content:</p><p>```language-json<br>{<br> “name”: “nodejs-pagination”,<br> “version”: “0.0.1”,<br> “dependencies”: {<br> “ejs”: “~0.8.4”,<br> “express”: “~4.2.0”<br> },<br> “engines”: {<br> “node”: “&gt;=0.10.0”<br> }<br>}<br>```</p><p>Then we will need to install required dependencies using `npm install` in the terminal. It will install all the required dependencies under `node_modules` directory.</p><p>### Create Index File to load App<br>Ok, now we have our simple demo application ready, let’s create a file which we will be using to run the application. So, go ahead and create `index.js` file with the following content:<br>```language-javascript<br>var express = require(‘express’);<br>var app = express();</p><p>app.set(‘views’, ‘views’);<br>app.engine(‘html’, require(‘ejs’).renderFile);<br>app.set(‘view engine’, ‘ejs’);</p><p>app.get(‘/’, function(req, res){</p><p>//set default variables<br> var totalStudents = 80,<br> pageSize = 8,<br> pageCount = 80/8,<br> currentPage = 1,<br> students = [],<br> studentsArrays = [], <br> studentsList = [];</p><p>//genreate list of students<br> for (var i = 1; i &lt; totalStudents; i++) {<br> students.push({name: ‘Student Number ‘ + i});<br> }</p><p>//split list into groups<br> while (students.length &gt; 0) {<br> studentsArrays.push(students.splice(0, pageSize));<br> }</p><p>//set current page if specifed as get variable (eg: /?page=2)<br> if (typeof req.query.page !== ‘undefined’) {<br> currentPage = +req.query.page;<br> }</p><p>//show list of students from group<br> studentsList = studentsArrays[+currentPage — 1];</p><p>//render index.ejs view file<br> res.render(‘index’, {<br> students: studentsList,<br> pageSize: pageSize,<br> totalStudents: totalStudents,<br> pageCount: pageCount,<br> currentPage: currentPage<br> });<br>});</p><p>var server = app.listen(3000, function() {<br> console.log(‘Listening on port %d’, server.address().port);<br>});<br>```</p><p>Let me explain this file, it’s really simple ExpressJs setup.</p><p>- First we initialize the ExpressJs App.</p><p>- Then we create a list of about 80 students.</p><p>- Then with a really simple trick, we split list of students. (I done this just for demo purpose). In real world applications we will get data from remote server or local database.</p><p>- Using line: `currentPage = +req.query.page;` we set current requested page eg: `<a href="http://localhost:3000/?page=2`">http://localhost:3000/?page=2`</a>.</p><p>- Finally, we pass all de required data to our view file, which we are going to create in next step.</p><p>If you want to learn more about ExpressJs Framework, follow this link: [<a href="http://expressjs.com/guide.html](http://expressjs.com/guide.html)">http://expressjs.com/guide.html](http://expressjs.com/guide.html)</a></p><p>### Create View file<br>Now, last step of app is create view file, which we are going name as `index.ejs` in `views` directory.<br>```language-html<br>&lt;ul class=”list-group”&gt;<br> &lt;% students.forEach( function( student ) { %&gt;<br> &lt;li class=”list-group-item”&gt;&lt;%= student.name %&gt;&lt;/li&gt;<br> &lt;% }) %&gt;<br>&lt;/ul&gt;</p><p>&lt;% if (pageCount &gt; 1) { %&gt;<br>&lt;ul class=”pagination”&gt;<br> &lt;% if (currentPage &gt; 1) { %&gt;<br> &lt;li&gt;&lt;a href=”/?page=1&quot;&gt;&amp;laquo;&lt;/a&gt;&lt;/li&gt;<br> &lt;% } %&gt;<br> &lt;% var i = 1;<br> if (currentPage &gt; 5) {<br> i = +currentPage — 4;<br> } %&gt;<br> &lt;% if (i !== 1) { %&gt;<br> &lt;li class=”disabled”&gt;&lt;a href=”#”&gt;…&lt;/a&gt;&lt;/li&gt;<br> &lt;% } %&gt;<br> &lt;% for (i; i&lt;=pageCount; i++) { %&gt;<br> &lt;% if (currentPage == i) { %&gt;<br> &lt;li class=”active”&gt;&lt;span&gt;&lt;%= i %&gt; &lt;span class=”sr-only”&gt;(current)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;<br> &lt;% } else { %&gt;<br> &lt;li&gt;&lt;a href=”/?page=&lt;%= i %&gt;”&gt;&lt;%= i %&gt;&lt;/a&gt;&lt;/li&gt;<br> &lt;% } %&gt;<br> &lt;% if (i == (+currentPage + 4)) { %&gt;<br> &lt;li class=”disabled”&gt;&lt;a href=”#”&gt;…&lt;/a&gt;&lt;/li&gt;<br> &lt;% break; } %&gt;<br> &lt;% } %&gt;<br> &lt;% if (currentPage != pageCount) { %&gt;<br> &lt;li&gt;&lt;a href=”/?page=&lt;%= pageCount %&gt;”&gt;&amp;raquo;&lt;/a&gt;&lt;/li&gt;<br> &lt;% } %&gt;<br>&lt;/ul&gt;<br>&lt;% } %&gt;<br>```<br>To complete the tutorial about expample include basic pagination. This is a simple EJS Embedded JavaScript file. More info on EJS can be found here: [<a href="http://embeddedjs.com/](http://embeddedjs.com/).">http://embeddedjs.com/](http://embeddedjs.com/).</a></p><p>This is really basic example to demonstrate, you can amend it according to your needs.</p><p>### Run the application</p><p>Finally, to run the application and to see NodeJs Pagination in action run the following command in your terminal:</p><p>`node index.js`</p><p>Once it’s done, visit the page: <a href="http://localhost:3000/">http://localhost:3000</a> . On this page try to go different pages by clicking on pagination.</p><p>Hopefully this tutoral will help you in creating a really simple pagination for your nodejs application.</p><p>If you are looking for full source code from this tutorial, I have pushed it to github: [<a href="https://github.com/perminder-klair/nodejs-pagination-demo](https://github.com/perminder-klair/nodejs-pagination-demo)">https://github.com/perminder-klair/nodejs-pagination-demo](https://github.com/perminder-klair/nodejs-pagination-demo)</a> .</p><p>Any question regarding tutorial? Feel free to ask by using comment box below.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a2d787b77581" width="1" height="1" alt=""><hr><p><a href="https://medium.com/digitalaholic/simple-nodejs-pagination-a2d787b77581">Simple NodeJs Pagination</a> was originally published in <a href="https://medium.com/digitalaholic">DigitalAholic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>