Next.js Discord

Discord Forum

Reading Data Based on URL

Unanswered
WhyFencePost (Ping Reply) posted this in #help-forum
Open in Discord
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
@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
@Giant Angora sure
sure, here it is:
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 there
what 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.jsx
are you typescript fan?
typescript is ok with me
Giant Angora
very nice ❤️
so go ahead and create the ruote
got it...?
yes
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 issues
e.g.
when browser requests /favicon then your /app/id route will be fired
makes sense
Giant Angora
so thats y app/scans/id
makes sense
Giant Angora
show ...
ik what im doing
enough
to do that
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.tsx
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