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

So is the app.post meant to post the /home file?

This doesn’t work but am I still on the right track?

1 Like

I think you should render /public/trolled/home/index.html, instead of simply redirecting to /home/

Like this?

1 Like

No, rendering [dirname] + “/public/trolled/home/home.html”, not /home/ :sweat_smile:
Edit: home.html not index.html

Did this but still nothing

1 Like

Oops not index.html but home.html

Still doesn’t work but also will it automatically redirect to /home?

Think of it this way.

app.get is a computer getting data from a server
app.post is a computer pushing data to a server

in your code above, you are sending a file named home. You need to send /public/trolled/home/home.html, or if that does not work /trolled/home/home.html

This should be inside a app.get

2 Likes

:+1:

I tried both of those but still says cannot POST /home
image

you have too many return statements by the way

1 Like

Nothing but do you think the req.session.user could be making it screw up

what kind of nothing are you getting? http 502?

Cannot POST /home but if i put /home at the end of the url it shows error ENOENT no such file or directory, stat ‘/app/home’

okay I see you’ve removed the redirect… and this new code looks wrong in many ways. other than the aforementioned multiple returns,

  • doing app.get in a handler doesn’t redirect or serve that other file. it adds a new handler while the app is in the middle of responding. definitely not what you want to do
  • plus that new handler is going to respond to poeple who go to whatever.glitch.me/public/trolled/home/home.html rather than whatever.glitch.me/home.

sorry to say, but you’re getting way off track trying to follow many different people’s vague suggestions

2 Likes

Here is what you need to serve a web page:

//app.get
app.get("/", (req, res) => {
  res.sendFile(__dirname + "/NavToContainingDirectory/changethistoyourhomepage.html");
});

change the “/” in the app.get to where you want the page to show up, and change the text after __dirname to your HTML file your want to serve.

1 Like

Yup on the first handler you should only have “/home/”, and the too many returns in the code can probably mess up with it.

Ok so, there is also a res.send that sends the login page with the form data and when you submit it it sends this


So should I instead send the POST data to another page that checks the pass email and other stuff which then redirects to the /home with the user data?
(I found this out because no matter what I put in the if (isValid) string it would always say cannot post /home).

sounds like your design for this has changed since we last talked. previously you were going to have the browser POST to /login and then redirect to /home. but now you’re having it post directly to /home? why the change?

I don’t think it changed because I’ve had this code before except I didn’t think it was relevant and it was posting to /home, I had the form data hosted on /login and then it matches it with the database and sends that data to /home which then makes a session for the user.

Here’s what it looks like on my project right now
Screen-recording-2023-02-14-1.47.22-PM

how did you get/write this code that you have in server.js? it looks like it’s trying to do something else

I used some Chat GPT

now I’m curious how it suggests to fix this :laughing:

What happens(I think) Is that when it uses res.send it sends a post to /home and redirects to there but because it’s a /post It doesn’t load the page or something like that which is why it says cannot POST /home

I just wanna have it POST and GET /home at the same time

tell them that this creates a race condition, and the process of logging in may or may not finish by the time it tries to check if the user is logged in

Well how do I have it post to something which checks the db and then lets them in if its correct which then redirects them to the /home page with the login info

I can’t simply use /POST because I need to use /GET to get the actual page but /GET doesn’t put the form data anywhere

what I usually see is you’d have the browser do the POST, then handle the login on the server, then the server redirects to /home, which causes the browser to send a second request, which is a GET

Just following up, did you get it figured out @Jhondoe2122?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.