Hello everyone,
Recently, my journey with NodeJS just commenced and I have been exploring API routes in NextJS as it provides an easy setup and clear visibility of the processes. While I have a grasp on creating basic get requests, I am now intrigued by something a bit more complex.
My current endeavor involves constructing an API route that can be filtered by page number. For instance, api/pages/1
should retrieve page 1, and so forth.
Here is a snippet from my file located at /api/game.js
:
export default async function handler(req,res) {
const response = await fetch('https://exampleapi.com/pages/?sort=&page=1&per_page=50')
const jsonData = await response.json();
res.status(200).json(jsonData);
}
This code functions properly, but my goal is to create dynamic routes for all pages since the number of pages in the external API can vary. To tackle this, I have introduced another folder with a file named [gamepage].js
. However, I am uncertain about how to modify the fetch call within the game.js
file here:
export default async function handler(req, res) {
const { pno } = req.query
console.log(pro)
const response = await fetch(`https://exampleapi.com/pages/?sort=&page=${pno}&per_page=50`)
const jsonData = await response.json();
res.status(200).json(jsonData);
}
Apologies if this question seems too rudimentary, as I am still navigating my way through backend JavaScript.