简体   繁体   English

Kraken.js 404和500个中间件模板

[英]Kraken.js 404 and 500 middleware templates

Is it possible to use different templates for 404 and 500 errors in a kraken.js application? 在kraken.js应用程序中是否可以针对404和500错误使用不同的模板? 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. 我可以自己修改中间件以接受多个模板,并通过某种逻辑来选择哪个模板,但是我想知道是否还有其他解决方案。

Answered here on the kraken github repo: https://github.com/krakenjs/kraken-js/issues/434 在kraken github仓库上回答这里: 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. 对于特定于路线的404页面,您可以直接在要具有不同404模板的路线上使用kraken 404中间件。 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. 这很棒,因为我在config.json中配置的404模板将是默认模板,并且对于我希望逐条路由的任何内容都可以使用上述方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM