How can I get rid of the ".html" at the end of the url on every page

Hello!

I am building a website with express and I want to get rid of the “.html” at the end of each url.

I cant use the folder method.

And i tried to use the method with this code for each page

app.get("/home/", (request, response) => {
  response.sendFile(__dirname + "index.html");
});

But that didnt work either.

What else can I do?

Thanks.

what kind of not working happens with that code? 404 when you go to /home/?

1 Like

yes that happens

I checked on __dirname Modules: CommonJS modules | Node.js v18.2.0 Documentation, and it says there’s no trailing slash. So you should probably revise the

__dirname + "index.html"

part

2 Likes

not great with nodejs and i just found this code on this forum so im not sure how to revise it.

What is all your code befote your ‘app.get’

1 Like

I’ve just got some code for my 404 page

var express = require("express"); var app = express(); app.use(express.static(__dirname)); app.use((req, res) => { res.status(404).sendFile(__dirname + "/404.html"); }); app.listen(8080);

Try this instead. Also always place your 404 code at the very end.

const express = require("express");
const app = express();
app.use(express.static("public"));

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});
// put the code for 404 here

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});

Make sure to make a folder called views and place your index.html file there

2 Likes

The website doesnt load anymore.

Im getting this error from glitch on a black background

  failed to start application on simply-travel.glitch.me

  This is most likely because your project has a code error.
  Check your project logs, fix the error and try again.  

Here is what my server.js file looks like now

const express = require("express");
const app = express();
app.use(express.static("public"));

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});



app.use(express.static(__dirname));
app.use((req, res) => {
  res.status(404).sendFile(__dirname + "/404.html"); 
});
app.listen(8080);

// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});


app.use(express.static(__dirname));
app.use((req, res) => {
  res.status(404).sendFile(__dirname + "/404.html"); 
});
app.listen(8080);

What is your project url?

Also get rid of (very bottom of your projects code)


app.use(express.static(__dirname));
app.use((req, res) => {
  res.status(404).sendFile(__dirname + "/404.html"); 
});
app.listen(8080);

delete your server.js and replace it with this:

const express = require("express");
const app = express();
app.use(express.static("public"));

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

app.use((req, res) => {
  res.status(404).sendFile(__dirname + "/404.html"); 
});


// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
  console.log("Your app is listening on port " + listener.address().port);
});


You need to use, for example, Express. You can find an example at Glitch.com

They are using express but they just didn’t set it up correctly.

I’ve just used the code you sent but now no subdirectories work at all

Okay… You need to make a folder called public and place all the CSS and javascript files there. Those files in there will then work with directories. Also don’t move server.js though,

Try remixing glitch express template

Can I just drag files into a folder? or do I have to make new files in the folder and copy and paste the code into each new file in the folder.

There is no drag and drop but if you click on the 3 dots next to a file and the file name editor pops up you can do something like this:

/game.html edit it to /public/game.html and hit enter

hold on wait, the css and js files are already their own folders, i could change the name of the css folder and do what you suggested to add the js files to the css folder and change its name to public

1 Like

Hey, thanks for the help but I just rewinded the project. I’m fine with it. This is a school project with other people working on it too and I can’t risk messing up the website as the subdirectories stopped working and the CSS wouldn’t work and it honestly scared me.

Cheers.

1 Like

Okay sorry I couldn’t get the complete solution!

1 Like

Don’t worry It’s no problem! :smile:

Thanks loads!

1 Like

you could view this, as it helped me a lot with removing them!

1 Like

Unfortunately, the suggestions on this page didn’t work for me either, the one with the folders caused my CSS to stop working. and the 2 express methods didn’t let the website load.

1 Like

do you mind sharing your project link?

1 Like

Yeah its https://simply-travel.glitch.me

returns me a “This site can’t be reached”, are you sure its correct?

sorry, thought it was .com not .me try the link from that post now

can I recap some of the really good advice we’ve seen in this thread?

  1. move the 404 handler to the bottom: you’ve done this
  2. make sure there’s a slash in constructs like __dirname + "/something.html": this was a minor detail and I wanted to ask if you got around to trying that

there have been much larger scale changes suggested, such as restructuring the projects to move pages into a ‘views’ directory, but I’d recommend seeing if you can get the above two working first before you decide whether to undertake these larger changes

3 Likes

I’m about to submit this project so there isn’t really any point anymore.

Just thought it would be a nice thing to have on the site.

Thanks, guys.

PS: I added wh0’s last post as the solution as it may help someone else.

2 Likes

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