Express won't get a subdomain file

On my project i’m trying to make it redirect to the /home page but when I put /home at the end of the url it shows “Cannot GET /home/”
app.get("/", function (request, response) { response.sendFile(__dirname + "/views"); }); app.get("/", function (request, response) { response.sendFile(__dirname + "/trolled"); }); app.get("/", function (request, response) { response.sendFile(__dirname + "/home"); });

Everything else but /home works but the files are arranged like so
image
If you want my project name is “samhamsemporium.”

1 Like

I think what you’d need is to redirect to /trolled/home/home.html. or maybe move the file to public/home/index.html

…or add an app.get("/home", ... )

1 Like

I previously had the /home file by itself and not in the /trolled file and it did the same thing but, When i put app.get("/home", . . .) it shows cannot POST /home instead of cannot GET /home/.

1 Like

If I put https://samhamsemporium.glitch.me/home/ manually in the browser without logging in it says Error: ENOENT: no such file or directory, stat '/app/home'

1 Like

oh you’re using sendFile only with no static middleware?

for the ENOENT, make sure the path you put in the sendFile matches the directory layout of your project files

regarding the “cannot POST” thing, is the route meant to handle a form submission?

1 Like

For the static middleware do you mean: app.use(express.static("public"));
and is this a solution for the sendfile thing? app.get("/", function (request, response) { response.sendFile(__dirname + "/trollled/home"); });
(you can go to my project to see the code btw Glitch :・゚✧)

1 Like

And for the “cannot POST” It’s handling a form submission from a .post, then checks a database if it’s valid and does a redirect to /home with a session for the user if (isValid) { return res.redirect("/home"); return (req.session.user = "user"); } });
I know the code works because if the password isin’t correct it correctly displays “Incorrect password” and the redirect works too because I tried it on /trolled and it redirected to /trolled but its just the /home file not working.

1 Like

yup, that’s what I mean by the static middleware. it’s a different way from writing your own app.get( ... response.sendFile(...) ... ) for each URL you want to have. but it makes the URLs have things like .html at the end (there may be some way to configure it not to, dunno).

for the “cannot POST” It’s handling a form submission from a .post, then checks a database if it’s valid and does a redirect to /home with a session for the user

if you’ve written up this piece of code to do that database check, then it sounds like it’s not registered on the app object right.

1 Like

oh wait, it’s probably with the wrong kind of redirect after they log in.

this guide recommends using 303

res.redirect(303, "/home");
1 Like

Ah so what should I do to make the page actually display html content.
I know it’s not the redirect but i think it’s something to do with the middleware.

let’s try with the following:

  1. change the redirect to use HTTP 303 (see post Express won't get a subdomain file - #9 by wh0)
  2. leave the app.get("/home", ...) thing in (see post Express won't get a subdomain file - #3 by Jhondoe2122)

This makes it say “cannot POST /home”

@wh0 so does that mean I have to register it right? and how do I do that

wait so even with the HTTP 303, your browser is still making a POST request to /home?

your app.get thing is going to /, not /home. Try going to /home rather than just /

or just visit /

image

1 Like

My redirect is going to /home and when I put / in the browser it redirects to the front page of my project (Which I want it to do) and when I go to /home manually in my browser it says Error: ENOENT: no such file or directory, stat '/app/home BUT when I use the login button and put in the form data the error is Cannot POST /home which leads me to believe that it’s an error with it grabbing the data from the database but I don’t know because i’m new to this. Also I know the login code is correct because when I enter an incorrect username and pass it says “incorrect password” but if i enter it correctly it says the cannot post thing.

1 Like

Is your file home.html?

1 Like

You also need an app.post to post to an endpoint

1 Like

No its just /home but have have home.html in it but here’s how it looks

1 Like