Search code examples
javascriptnode.jsexpressmiddlewarekraken.js

Kraken.js 404 and 500 middleware templates


Is it possible to use different templates for 404 and 500 errors in a kraken.js application? Below is how you enable the middleware and tell it what template to use. For my application I need to use different templates depending on what section of the site the user is in.

"fileNotFound": {
  "enabled": true,
  "priority": 130,
  "module": {
    "name": "kraken-js/middleware/404",
    "arguments": ["tamarin/errors/404"]
  }
},
"serverError": {
  "enabled": true,
  "priority": 140,
  "module": {
    "name": "kraken-js/middleware/500",
    "arguments": ["tamarin/errors/500"]
  }
},

I could modify the middleware myself to accept multiple templates and some sort of logic to choose which template but I'm wondering if there is another solution.


Solution

  • Answered here on the kraken github repo: https://github.com/krakenjs/kraken-js/issues/434

    ...

    For route specific 404 pages you can just the kraken 404 middleware directly on the routes you want to have different 404 templates for. Here is how I accomplished that.

    var _404  = require('kraken-js/middleware/404');
    
    module.exports = function(router) {
    
        router.get("/:lang?/*", _404('tamarin/admin/404'),  function(req, res) {
    ...
    

    This is great because the 404 template I configured in the config.json will be the default template and for anything I want on a route by route basis can use the above approach.