production api routes not found 404
Answered
Brown bear posted this in #help-forum
Brown bearOP
Api routes work in local dev envrionment, but not in prod
81 Replies
@Brown bear Api routes work in local dev envrionment, but not in prod
how do they not work? from what i see they are built normally
Brown bearOP
oh
for some reason my api routes work in local
but no in prod
tbh not sure what the error is, but api routes was my first guess
When i click "guest" it should redirect me
but no redirect is happening
from this much information i cannot say anything
Brown bearOP
Request is not even going through
but no error message
@Brown bear Click to see attachment
clear the filter
Brown bearOP
jesus thank you
I dont understand why my
router.push() will not workfetch is successful
Brown bearOP
export const getServerSideProps = withIronSessionSsr(async function (ctx) {
const { req } = ctx;
console.log(req);
console.log(req.session);
if (req.session && req.session.user) {
const { username } = req.session.user;
const user = await prisma.user.findUnique({
where: {
username,
},
});
const { json } = serialize(user);
return { props: { user: json } };
}
return {
redirect: {
permanent: true,
destination: '/',
},
};
}, sessionOptions);const playAsGuest = async () => {
try {
const result = await fetch('/api/guest', {
method: 'POST',
credentials: 'include',
});
if (result.ok) {
router.push('/multiplayer');
return;
}
} catch (error) {
console.log(error);
}
};@Brown bear const playAsGuest = async () => {
try {
const result = await fetch('/api/guest', {
method: 'POST',
credentials: 'include',
});
if (result.ok) {
router.push('/multiplayer');
return;
}
} catch (error) {
console.log(error);
}
};
code looks okay. i can also see the multiplayer page in your network tab. i'm assuming you're not using Next 13, yeah?
Brown bearOP
"next": "13.4.2",there no error messages ...
@Brown bear Click to see attachment
can you see some html when you click on the preview/response tabs?
Brown bearOP
no:[
not sure what the issue is. although with the app directory in Nextjs 13, there's a new way to fetch data inside your components which is a bit different from the getServerSideProps
you may wanna try that first - https://nextjs.org/docs/app/building-your-application/data-fetching/fetching#async-and-await-in-server-components
Brown bearOP
i am using pages
no app
When you request this page on client-side page transitions through next/link or next/router, Next.js sends an API request to the server, which runs getServerSidePropsah okay. try clearing your cache. is your fetch successful here -
if (result.ok)Brown bearOP
i can clear cache by deleteing .next directory and rebuilding?
@Brown bear i can clear cache by deleteing .next directory and rebuilding?
i meant in your browser. right click the refresh/reload button while your console is opened and click clear cache
Brown bearOP
ohhhh
yea still not working
could this be something with iron sessoin?
at this point it doesnt seem like a network error
my user is always null, even though it should have a user
@Brown bear could this be something with iron sessoin?
don't think so. does your
playAsGuest() depend on any data from the getServerSideProps?Brown bearOP
no
how/where are you calling
playAsGuest() too?Brown bearOP
calling
playAsGuest() from / and if (result.ok){ router.push('/multiplayer}it works fine when im in single player mode and I push router
the only difference between multiplayer and singleplayer is using the api and iron session
@Brown bear calling playAsGuest() from / and if (result.ok){ router.push('/multiplayer}
so
playAsGuest isn't attached to a button or sth?Brown bearOP
oh
its attached to button
, no form
<button onClick={playAsGuest} className='basicButtonStyle text-xl'>
Guest
</button>is the button inside a form?
Brown bearOP
no
but it doesn't have to be
it doesn't
try visiting the
try visiting the
/api/guest route as in localhost:3000/api/guestBrown bearOP
it returns 500 on screen
both in production and development
does
singleplayer use any api route? and does it return 500 when you visit the url too?Brown bearOP
singleplayer does not use any api route
and no, /singleplayer working as intended
what does your
api/guest look likeBrown bearOP
import { NextApiResponse, NextApiRequest } from "next";
import { prisma } from "@/db";
import { v4 as uuidv4 } from "uuid";
import { withIronSessionApiRoute } from "iron-session/next";
import { sessionOptions } from "@/session";
async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
try {
const rand = uuidv4();
const guest = `Guest-${rand}`;
const payload = {
username: guest,
password: rand,
};
const userCreated = await prisma.user.create({
data: payload,
});
if (userCreated) {
req.session.user = userCreated;
await req.session.save();
return res.send(200);
}
res.send(400);
} catch (error) {
console.log(error);
res.send(500);
}
} else if (req.method === "PUT") {
try {
req.session.destroy();
return res.send(200);
} catch (error) {
console.log(error);
res.send(500);
}
} else {
res.send(500);
}
}
export default withIronSessionApiRoute(handler, sessionOptions);it may be something with your prisma/iron session config. you mentioned the other time that the user wasn't being created, yeah?
@Brown bear Click to see attachment
here, what's the payload for your guest request?
Brown bearOP
There is no payload
Im not sure why im not getting an error
No network error
No user error
Brown bearOP
ok i think i found out why
my database is not connecting
Can't reach database server at `localhost`:`5432`
Please make sure your database server is running at `localhost`:`5432`.
at An.handleRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:174:7205)
at An.handleAndLogRequestError (/var/task/node_modules/@prisma/client/runtime/library.js:174:6358)
at /var/task/node_modules/@prisma/client/runtime/library.js:177:2908
at async /var/task/node_modules/@prisma/client/runtime/library.js:177:3123
at async t._executeRequest (/var/task/node_modules/@prisma/client/runtime/library.js:177:10621)
at async handler (/var/task/.next/server/pages/api/guest.js:71:33) {
clientVersion: '4.15.0',
errorCode: undefined
}had to dig through vercel logs
ah okay
once you sort that out it should work
Brown bearOP
im having trouble looking for instuctions on how to set that up
Google/chat GPT should help out. not very familiar with Prisma
Brown bearOP
!solved
Brown bearOP
had to get my db connection right
Answer
Brown bearOP
also separated dev and prod db
thank you for your help~