How to access Server side variable on Client side page (Loginpage)
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
Seriously how do i send a string variable from a client side page like localhost:3000/loginpage
to a backend/serverside file that checks whether or not that string exists, and if it exists it returns true.
to a backend/serverside file that checks whether or not that string exists, and if it exists it returns true.
52 Replies
you can create a route handler for this. And then fetch this handler clientside
@B33fb0n3 you can create a route handler for this. And then fetch this handler clientside
Masai LionOP
what do you mean by "handler"?
Masai LionOP
Do you know of any Youtube video that explains this well, i have read through some of it but i don't fully understand.
yea, this videos describes it in a good way: https://www.youtube.com/watch?v=vrR4MlB7nBI
@B33fb0n3 yea, this videos describes it in a good way: https://www.youtube.com/watch?v=vrR4MlB7nBI
Masai LionOP
Do you think you could make a quick example of how to use the get and post in nextjs
@Masai Lion Do you think you could make a quick example of how to use the get and post in nextjs
yea, this is a GET:
And this is a POST:
export async function GET() {
// do stuff
return Response.json({ data })
}And this is a POST:
export async function POST() {
// do stuff
return Response.json({ data })
}@Masai Lion Seriously how do i send a string variable from a client side page like localhost:3000/loginpage
to a backend/serverside file that checks whether or not that string exists, and if it exists it returns true.
what is your setup? do you know how to send data to the backend?
@aardani what is your setup? do you know how to send data to the backend?
Masai LionOP
nope thats what is confusing me
this is in the app directory
do you know Next.js can be used as a backend?
Masai LionOP
yes is that not the point of nextjs other than to use react?
soooo do you know how to create a route in next.js?
Masai LionOP
yes i think so
you mean like page.tsx and dynamic routes?
is there a
app/api/adminLogin/route.ts in your project?have you created that file?
Masai LionOP
no it is app/api/adminLogin.ts
so i got the routing wrong?
yeah
next.js dont consider that a valid route
it has to be a route.ts in the app dir
it has to be a route.ts in the app dir
Masai LionOP
here is the get btw
but im pretty sure idk how the get stuff works either
ait lemme fix it up
wym "how the get stuff works"?
Masai LionOP
i mean like idk how (GET/POST) works literally
like ive used fetch a few times and fetched json a few times before but that is basically it
thats my experience using async
anyways, so the api route needs to be called the same as the page route?
and i basically know nothing about http methods
@Masai Lion i mean like idk how (GET/POST) works literally
GET and POST are just a part of the HTTP convention.
people collectively decided that a connection should be identified by types of "Method". Those methods can be GET, POST, PUT, DELETE, etc.
by writing
people collectively decided that a connection should be identified by types of "Method". Those methods can be GET, POST, PUT, DELETE, etc.
by writing
export default function GET() you are telling Next.js backend to "open" up a route for that particular path and tell them what to do if client request to the backend using GET https://localhost:3000/api/adminLoginfetch() by default will request to the server using GET method
Masai LionOP
ok now im beginning to understand, lemme try it out.
yes, its better if you just ask whats confusing you rather than having me guess which part ure confused about
@aardani yes, its better if you just ask whats confusing you rather than having me guess which part ure confused about
Masai LionOP
Yeah sorry about that.
no worries !
Masai LionOP
Ok so i have actually got a different result
last time it was just not found
oh it has to be
that error means the route is valid and found (yay!) but no method is allowed
export function GET() not export default function GET()that error means the route is valid and found (yay!) but no method is allowed
my bad
Masai LionOP
ooh
so no default
@B33fb0n3 this one: https://nextjs.org/docs/app/building-your-application/routing/route-handlers
now that you understood, i suppose you can take a look at this?
Masai LionOP
does it need something like 'use server' or something?
yeah im gonna reread in a sec
@Masai Lion does it need something like 'use server' or something?
no, this is for creating an isntant endpoint in your front-end code
you could use that if you dont plan to make routes thats avaialble for outside of your next.js application
coz currently, by using GET, you are opening an url thats accessible by other people