404 status for catch-all route

Based on my own stupidity trying to figure out something that didn’t really need figuring out I found a small omission in routing.

To be compliant with http status codes the catch-all route should set a 404 status on the response. Right now it will send a 200 OK.

There should probably be an option in the routing panel to set the status to 404 for a catch-all route(*)

To workaround this, instead of creating the catch-all on the routing panel, create a small middleware and place it in extensions/server_connect/routes/404.js

exports.after = function (app) {
    app.get('*', (req, res) => {
        res.status(404).sendfile('404.html', { root: 'public' });
    });
}

This will render the 404.html file stored in /public and set the response to 404.

Brilliant Jon,L

You wouldn’t say that if you had seen what I was trying to achieve one hour before I figured out the workaround.

Excellent work! I’m surprised there is no native component for this in Wappler.

Will an 404.ejs file work instead of 404.html …?

No need for a component.

Create a 404.ejs page. Add the path * as the last entry in routing (it must be the last entry in routing so don’t foeget to drag it to the bottom after you create other pages otherwise those pages will not route).

You would get a 200 OK response with that.

1 Like

Indeed, just noticed is the response not the redirect. I stand entirely corrected.