我正在通过Swift Express编写最简单的服务器
我想为“url not found”添加自定义处理程序,这样如果用户写“/ notFoundUrl”,他会看到类似的内容:“Url”notFoundUrl“找不到,请转到主页”.
我已经添加:
app.get("/:notFoundUrl+") { (request:Request<AnyContent>)->Action<AnyContent> in
print(request.params["notFoundUrl"])
return Action<AnyContent>.render("index", context: ["hello": "Page Not Found: " + request.params["notFoundUrl"]!])
}
但这不合适因为:
>订单确实很重要.
>我无法返回404错误.
那么如何在Swift Express Server中添加自定义“Page Not Found / 404 handler”?
解决方法:
它比你试图做的简单得多.
/// Custom page not found error handler
app.errorHandler.register { (e:ExpressError) in
switch e {
case .PageNotFound(let path):
return Action<AnyContent>.render("404", context: ["path": path], status: .NotFound)
default:
return nil
}
}
完整的操作可以在这里找到:https://github.com/crossroadlabs/Express/blob/master/doc/gettingstarted/errorhandling.md
原文地址:https://codeday.me/bug/20190702/1355397.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。