Reading Data Based on URL
Unanswered
WhyFencePost (Ping Reply) posted this in #help-forum
I am creating an interactive game site (what it is is not important) and I need a way to grab some very basic data from a file (i need to grab a page title, a url to a page, and an array of text strings). I need to get this based on the url, so that I can make a url of
/app/[scan code here] and then it will read the data corresponding to the scan code that was there, so that the page can use it. How would I do this? I would assume middle-ware?51 Replies
Giant Angora
hi @WhyFencePost (Ping Reply)
you cannot load data inside middleware. you have only one file, why not you just import it statically and on the base of pathname, find your required data in file
you cannot load data inside middleware. you have only one file, why not you just import it statically and on the base of pathname, find your required data in file
@WhyFencePost (Ping Reply) How would I do the data access based on pathname?
Giant Angora
Could you show me app dir structure…?
Not rn I’m on a phone but when I’m home yea
despite this, I dont quite think that you get my goal. My goal is t be able to have a url (in browser) of
\app\6778 or \app\6829 or any other 4 numbers, and to then to be able to read from a coresponding set of data (how that data is organised will depend on what I can do). I want to be able to write one page (since writing a page per number would be tedious), and then use the number to diffrentiate. The final thing is that if the number is not supported (not in the data set I would like to output an id not found page)Giant Angora
you dont have
/[dynamic] route therewhat is a dynamic route?
Giant Angora
are you really beginner?
these are my questions
Giant Angora
i have got your pov
@Giant Angora are you really beginner?
not really, i have done a lot, tho never anything with a database like this
Giant Angora
no problem
well thank ou
Giant Angora
what you name your 4 digit number?
is it any id?
scan id was my idea (since you scan something to get it)
Giant Angora
create this route
- app
- scans
- [scanId]
- page.tsx or page.jsxare you typescript fan?
typescript is ok with me
Giant Angora
very nice ❤️
so go ahead and create the ruote
got it...?
why scans above that?
i just want it to have the route of \app\6768
so would that be \app\app\scanid
Giant Angora
well it is possible
/app/id but it will create issuese.g.
when browser requests /favicon then your /app/id route will be fired
makes sense
Giant Angora
so thats y
app/scans/idmakes sense
@Giant Angora create this route
- app
- scans
- [scanId]
- page.tsx or page.jsx
made the route as depicted here
Giant Angora
show ...
Giant Angora
thats it... now open page.tsx and get params there
import file and on the base of params you can filter data... if not found you can call
notFound() and for that you need extra page not-found.tsxsure, i can do that in a sec, i assume i do this: https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes
thank you for your help
Giant Angora
no problem 😊
should i use router or pages for this?
(i dont even know what those 2 are)
Giant Angora
use app router always
sounds good
Giant Angora
app router uses server components first
you need to know a lot and i appreciate your efforts