From the beginning of my project, when I started it up I continuously get 404 for user, Cannot GET /api/user
I have a SQLite database set up with Prisma, but for some reason this code block is not working for me:
routes.get("/api/user", async (req, res) => {
const { id } = req.session;
if (!id) {
return res.json({
users: null,
});
}
const user = await client.user.findUnique({
where: { id },
});
if (!user) {
return res.json({
user: null,
});
}
res.json({
user: {
id: user.id,
email: user.email,
},
});
});
It's been awhile since I developed a backend API, so I may have missed some detail.
My server starts up just fine:
import "./env.mjs";
import express from "express";
import cookieSession from "cookie-session";
import routes from "./routes.mjs";
const initServer = async () => {
const app = express();
app.use(express.json());
app.use(
cookieSession({
name: "session",
keys: [process.env.COOKIE_SECRET],
httpOnly: true,
sameSite: "strict",
})
);
app.use((req, res, next) => {
console.log(req.method.toUpperCase(), "-", req.url);
next();
});
app.use(routes);
const PORT = 8000;
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
};
initServer();
After I built my sign up endpoint along with the frontend sign up form, I tried to sign up and I cannot post either, I continue to get a 404. I am unable to access any of my api endpoints.
I am following the documentation here:
I ran the server by itself on port 8000 and with Postman did a get request to localhost:8000/api/user
and I was able to successfully get { user: null }
. So I believe the issue is on the React side, but not sure what it is. I am supposed to be connecting React to Express via a proxy like so:
"options": {
"allowedHosts": [
"localhost"
],
"proxy": "http://localhost:8000",
},
And I had it set up like that because when I only had:
"proxy": "http://localhost:8000"
I kept getting this error in terminal:
- options.allowedHosts[0] should be a non-empty string.
I feel as though by attempting to resolve that proxy issue, I did not really resolve anything, but just kind of added another layer that makes it difficult to discern what is going on.
In my network tab, I get a 404 for http://localhost:3000/api/user, should it be looking referencing localhost:8000 instead? Is it not proxying?