Browse Source

Url pattern for pagination on main page

pull/3/head
xcps 3 years ago
parent
commit
2e68de00d8
  1. 2
      app.js
  2. 28
      routes/index.js

2
app.js

@ -30,11 +30,11 @@ app.use(bodyParser.urlencoded({ extended: false })); @@ -30,11 +30,11 @@ app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/address', address);
app.use('/transaction', transaction);
app.use('/block', block);
app.use('/search', search);
app.use('/', index);
// catch 404 and forward to error handler
app.use(function(req, res, next) {

28
routes/index.js

@ -35,8 +35,12 @@ function formatRate(bytes, decimals = 2) { @@ -35,8 +35,12 @@ function formatRate(bytes, decimals = 2) {
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
function getPagination(count, page) {
const pagesCount = count > BLOCKS_PER_PAGE ? Math.ceil(count / BLOCKS_PER_PAGE) : 0;
function getMaxPage(blockCount) {
return blockCount > BLOCKS_PER_PAGE ? Math.ceil(blockCount / BLOCKS_PER_PAGE) : 0;
}
function getPagination(blockCount, page) {
const pagesCount = getMaxPage(blockCount)
let pagination = null;
if (pagesCount) {
pagination = {
@ -67,10 +71,21 @@ function getPagination(count, page) { @@ -67,10 +71,21 @@ function getPagination(count, page) {
}
/* GET home page. */
router.get('/:offset*?', async function(req, res, next) {
router.get('/:page(\\d+)?', async function(req, res, next) {
const paramPage = parseInt(req.params.page);
const blockCount = await models.Block.count();
const maxPage = getMaxPage(blockCount)
let page = isNaN(paramPage) || paramPage < 1 ? 1 : paramPage;
if (page > maxPage) {
page = maxPage;
res.redirect(`/${page}/`);
return;
}
const pagination = getPagination(blockCount, page);
const paramPage = parseInt(req.params.offset);
const page = isNaN(paramPage) || paramPage < 1 ? 1 : paramPage;
const offset = BLOCKS_PER_PAGE * (page - 1);
const blocks = await models.Block.findAll({
@ -80,9 +95,6 @@ router.get('/:offset*?', async function(req, res, next) { @@ -80,9 +95,6 @@ router.get('/:offset*?', async function(req, res, next) {
offset,
});
const count = await models.Block.count();
const pagination = getPagination(count, page);
blocks.forEach((arrayItem) => {
arrayItem.ago = arrayItem.time.toUTCString().substring(5);
arrayItem.difficulty = parseFloat(arrayItem.difficulty).toFixed(8);

Loading…
Cancel
Save