How would i create a redirect 404 page for many pages?

I would like to know how to create a custom 404 redirect page, i have already did this but when i create new pages and go to them the 404 error pops up. The original source was from A way to make custom 404 page for glitch website applications?

Same way with routing, but the address would be *

How do I handle 404 responses?

In Express, 404 responses are not the result of an error, so the error-handler middleware will not capture them. This behavior is because a 404 response simply indicates the absence of additional work to do; in other words, Express has executed all middleware functions and routes, and found that none of them responded. All you need to do is add a middleware function at the very bottom of the stack (below all other functions) to handle a 404 response:

app.use(function (req, res, next) {
  res.status(404).send("Sorry can't find that!")
})

Add routes dynamically at runtime on an instance of express.Router() so the routes are not superseded by a middleware function.

1 Like

That is not how it is done. Or the way I do it.

how do you do it then?

Default code for a page, replace the / with a *

1 Like
app.get("*", (req, res) => {
    res.send("Error: 404")
});

This may work

1 Like

ThT is what I said. :stuck_out_tongue:

Howdy I am Gonna be direct!

If you want to Have a HTML File Load if a Page is Not found You can use
This Little Snippit of code in your Server.js (or with ever file starts your page)

app.use(function(req, res, next) {
res.status(404).sendFile(__dirname + “/Your_page_Name.html”);

Hope i helped
});

@Boofhead1000, isn’t that what @SudhanPlayz said?

@khalby786 My code Calls a File If it unable to find the page The Other one Just displays Text on the Screen
So you have 2 Options!

@SudhanPlayz Code:

app.get("*", (req, res) => {
    res.send("Error: 404")
}); 

Which Just Puts Small Text on the screen Simple or
Mine:

app.use(function(req, res, next) {
res.status(404).sendFile(__dirname + “/Your_file_path.file”);
});

Which you can Use to Call a File to Display

So kinda what he said But My code Does Something Different
Just wanted add to This Page

1 Like

idk if its just me bot it says app undefined

make sure you have

const express = require("express")
const app = express()

at the top of your code.

A note on the function above, it will only work if you put it at the bottom of your file

1 Like