matthiaaas / express-file-routing

Flexible file-based routing for Express (like Next.js + additional features)
https://www.npmjs.com/package/express-file-routing
MIT License
83 stars 13 forks source link

Add support for error routes #19

Closed Azoraqua closed 1 year ago

Azoraqua commented 1 year ago

Allowing to display said error pages based on a few predefined error states.

Example:

Perhaps this is beyond the scope of this project, but perhaps there could be made a way to hook into such logic?

matthiaaas commented 1 year ago

hey @Azoraqua, thank you for filing an issue. Under the current implementation, you would have to add an error middleware/route handler after the router() invocation.

app.use(router()) // or createRouter(app)

// do whatever you want here
// like defining additional handlers
app.get("/foo", async () ...)

// actual error handling middleware
app.use(async (err, req, res, next) => {

})

I think that this behavior is desirable. It allows you to append as many router() instances to an app or even define additional native Express handlers as you want before declaring all error handling.

Also, I think that response error handling is something that should be logically separated from the actual routing. Unlike other file routing implementations in (frontend web) frameworks, this library does not try to force you to solely use file-based routing, but is supposed to be compatible with other routing strategies in a project that run in parallel.

But, I agree that for some projects, your suggestion is an absolutely valid and straight-forward approach. I'm just unsure if this should rather be outsourced to another plugin-like library that extends this package.

Let me know what you think.

Azoraqua commented 1 year ago

I am fine with either option, optionally it could be a simple configuration option that handles it.