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
If you want my project name is “samhamsemporium.”
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/.
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'
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 :・゚✧)
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.
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.
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.
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.
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
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
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.
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
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
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