'ExpressJS - Handle multiple pages/pagination

I am trying to handle multiple pages for a search with expressjs but it seems that doesn't work at all. I have the root and the query params

/properties/search?location=London&page=0

I did a button for next page and when summits I have multiple queries parameters

/properties/search?location=London&page=0&page=1&page=2

How can I have a single query for page like:

/properties/search?location=London&page=0
/properties/search?location=London&page=1
/properties/search?location=London&page=3

Here is my code

                               <a type="submit" name="page" value="<%= currentPage+1 %>">
                                    <div class="ui animated button large" style="background-color:#F76C6C;" tabindex="0">
                                         <div class="visible content">Next Page</div>
                                         <div class="hidden content">
                                              <i class="left arrow icon"></i>
                                         </div>
                                    </div>
                               </a>


Solution 1:[1]

Here's an example of pagination using ExpressJS.

const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const bodyParser = require('body-parser');

const app = express();

app.use(cors());
app.use(morgan(':method :url :status :user-agent - :response-time ms'));
app.use(bodyParser.json());

app.get('/properties/search', async (req, res) => {
    try {
        let items = Array.from(Array(10000).keys())
        const maxSize = 20;
        const page = parseInt(req.query.page) || 0;
        const resultItems = items.slice(page*maxSize, (page+1)*maxSize);
        res.send(resultItems.join('<br>'));
    } catch (err) {
        console.log(err);
        res.status(500).json({ 'Error': 'Internal Server Error' });
    }
});

app.listen(process.env.PORT || 3000, function () {
    console.log('Express app running on port ' + (process.env.PORT || 3000))
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Trishant Pahwa