Question: By default, the Replit 404 page says “Not found.” I was wondering if it was possible to write code that redirects you to a 404 page if the requested page does not exist.
const express = require('express');
const path = require('path');
const app = express();
const port = 3000; // You can choose your own port number
// Serve static files from a directory (e.g., CSS, images)
app.use(express.static('public'));
// Define your 404 handler
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, 'errors/404.html'));
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
That is why. That template is intended for static HTML/CSS/JS sites. The code you provided above is Node.js code. Please try the Node.js template, and see if that resolves your issue.
Hello and welcome to the forums, @Gamer11!
There is a way (technically, though it is a bit out of the way), just not in Replit itself. You can still code a website or such in Replit, you just must use Replit’s built-in Git Tab. By using this you can setup a connection to a GitHub Repository, and by running the link on your GitHub Repository, you can create a custom HTML/CSS/JS 404 page.
If this interests you, just reply to this message and I’ll elaborate!
@Firepup650 The reason I gave my alternate solution is because some people may be hesitant to learn such extensive libraries such as NodeJS (even if in this case it isn’t too hard to pick up, it can still be easier sometimes to just stick with plain HTML, CSS and JS until you feel ready to move on.
You don’t have to really learn anything to use my template, just place your files in the public folder, and customize 404.html to your liking. You don’t have to touch the Node part at all.