[英]Next.js: How to create a dynamic route for all pages present from the API?
Greetings to everyone,向大家问好,
I have just started to learn NodeJS and have been experimenting on API routes in NextJS as its easy to setup and see what's actually going on, So I know how to make a basic get request but I'm interested in something a little complex.我刚刚开始学习 NodeJS,并且一直在 NextJS 中试验 API 路由,因为它易于设置并查看实际发生的情况,所以我知道如何发出基本的获取请求,但我对有点复杂的东西感兴趣。
So I'm trying to build an API route that can be filleted by page number, so for example api/pages/1
would return page 1 and so on.所以我正在尝试构建一个可以按页码填充的 API 路由,例如
api/pages/1
将返回第 1 页,依此类推。
so this is my file in /api/game.js
所以这是我在
/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);
}
Now this works obviously but I want to know how I can make dynamic routes for all the pages.现在这显然有效,但我想知道如何为所有页面制作动态路由。 Since I'm using an external API I'm not sure how many pages exist at any moment.
由于我使用的是外部 API,因此我不确定任何时候存在多少页。
So far I have created another folder and called the file [gamepage].js
, I'm not sure how I would manipulate the fetch call in the game.js
file in here.到目前为止,我已经创建了另一个文件夹并调用了文件
[gamepage].js
,我不确定我将如何在这里操作game.js
文件中的 fetch 调用。
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);
}
Sorry if the question is too beginner level, I'm just getting started with backend JS.抱歉,如果问题太初级,我才刚刚开始使用后端 JS。
I got it to work, I was just missing the gamepage
from req.query
我让它工作了,我只是缺少
gamepage
中的req.query
const { pno } = req.query.gamepage
this did it.这做到了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.