My first suggestion is that you do not define your endpoint with /:variable
. This will then match any route you create that follows that pattern. You should instead use something like /pages/:page
to get a specific page
Then, you should use the URL parameter functionality in Express to include a URL parameter.
Define your route like this:
app.get("/pages/:page", function (req, res) {
var pg = undefined;
if (req.query.page) {
pg = req.query.page;
}
}
You can then access the page in req.query.page if it exists and do whatever you want with that value.
So for example, you could submit a request with localhost/pages/123?page=3
.
req.query.page
would equal 3 and req.params.page
would equal 123.