如何在 feathers.js 中自定义默认布局
How to customize default layouts in feathers.js
我正在使用 feathers.js v3.0.5
作为我的休息解决方案,使用 feathers-cli
创建应用程序
对于每个错误代码,框架都有一个特定的 html 模板,包括羽毛徽标和样式:
401 Not Authenticated
403 Forbidden
404 Not Found
Is there a way to customize the default templates for errors?
我想对于 index.html,我只能修改 /public/
文件夹中的 index.html
但是对于错误我没有找到任何明智的建议。
我认为 Feathers 应用程序不应该负责呈现错误页面。正如您所说,Feathers 是您的 REST/WebSocket 平台。大多数人将其用作前端应用程序的纯数据后端,甚至用作完整 Express 应用程序的 API 服务器。
在任何一种情况下,您都应该在代码中处理 404 错误(例如)并重定向/显示到您的 404 "UI"。
由于您没有详细说明您的应用、设置等,我无法提供任何细节。希望这有帮助。
您可以设置自定义 HTML 页面,如 Feathers error handler documentation 中所述:
const { errorHandler } = require('@feathersjs/express');
const app = feathers();
// Just like Express your error middleware needs to be
// set up last in your middleware chain.
app.use(errorHandler({
html: function(error, req, res, next) {
// render your error view with the error object
res.render('error', error);
}
}));
// Set paths to custom HTML pages
app.use(errorHandler({
html: {
404: 'path/to/notFound.html',
500: 'there/will/be/robots.html'
}
}));
另一个好的解决方案是关闭 html 错误。这将 return json 响应更容易被您的 api.
基于软件的消费者解读
例子
app.use(errorHandler({
html: false
}));
我正在使用 feathers.js v3.0.5
作为我的休息解决方案,使用 feathers-cli
对于每个错误代码,框架都有一个特定的 html 模板,包括羽毛徽标和样式:
401 Not Authenticated
403 Forbidden
404 Not Found
Is there a way to customize the default templates for errors?
我想对于 index.html,我只能修改 /public/
文件夹中的 index.html
但是对于错误我没有找到任何明智的建议。
我认为 Feathers 应用程序不应该负责呈现错误页面。正如您所说,Feathers 是您的 REST/WebSocket 平台。大多数人将其用作前端应用程序的纯数据后端,甚至用作完整 Express 应用程序的 API 服务器。
在任何一种情况下,您都应该在代码中处理 404 错误(例如)并重定向/显示到您的 404 "UI"。
由于您没有详细说明您的应用、设置等,我无法提供任何细节。希望这有帮助。
您可以设置自定义 HTML 页面,如 Feathers error handler documentation 中所述:
const { errorHandler } = require('@feathersjs/express');
const app = feathers();
// Just like Express your error middleware needs to be
// set up last in your middleware chain.
app.use(errorHandler({
html: function(error, req, res, next) {
// render your error view with the error object
res.render('error', error);
}
}));
// Set paths to custom HTML pages
app.use(errorHandler({
html: {
404: 'path/to/notFound.html',
500: 'there/will/be/robots.html'
}
}));
另一个好的解决方案是关闭 html 错误。这将 return json 响应更容易被您的 api.
基于软件的消费者解读例子
app.use(errorHandler({
html: false
}));