How to handle 404 pages when I'm also trying to catch urls with dynamic parameters? I.e., /:id
The code below never reaches the last rule for 404, and Express always tries to open html
file even when they don't exist.
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dev'));
app.get('/', function(req, res) {
res.sendFile(`${__dirname}/dev/index.html`);
});
app.get('/:id', function(req, res) {
res.sendFile(`${__dirname}/dev/pages/${req.params.id}.html`);
});
app.get('*', function(req, res) {
res.status(404).sendFile(`${__dirname}/dev/404.html`);
});
app.listen(3000, function() {
console.log('Express server started http://localhost:3000');
});