如何将环回 API 响应更改为特定格式
How to change loopback API response to a particular format
我是环回的新手 我想将来自我的环回远程方法 API 的每个响应更改为特定格式
例如:如果成功
{
status:1,
data:{},
message:"Success"
}
如果出错
{
status:0,
data:{},
message:"Something went wrong"
}
您应该创建一个引导脚本来更改所有远程方法响应:
创建 hook.js 或 /server/boot/
中的任何其他名称
module.exports = function (app) {
var remotes = app.remotes();
// modify all returned values
remotes.after('**', function (ctx, next) {
if (ctx) {
ctx.result = {
status: 1,
data: ctx.result,
message: "Success"
};
} else {
var err = new Error();
next({
status: 0,
data: err,
message: "Something went wrong"
});
}
next();
});
};
查看这些链接以获取更多信息:
格式化远程方法响应(最后一部分)
https://loopback.io/doc/en/lb3/Remote-methods.html
挂钩
我是环回的新手 我想将来自我的环回远程方法 API 的每个响应更改为特定格式
例如:如果成功
{
status:1,
data:{},
message:"Success"
}
如果出错
{
status:0,
data:{},
message:"Something went wrong"
}
您应该创建一个引导脚本来更改所有远程方法响应:
创建 hook.js 或 /server/boot/
中的任何其他名称module.exports = function (app) {
var remotes = app.remotes();
// modify all returned values
remotes.after('**', function (ctx, next) {
if (ctx) {
ctx.result = {
status: 1,
data: ctx.result,
message: "Success"
};
} else {
var err = new Error();
next({
status: 0,
data: err,
message: "Something went wrong"
});
}
next();
});
};
查看这些链接以获取更多信息:
格式化远程方法响应(最后一部分)
https://loopback.io/doc/en/lb3/Remote-methods.html
挂钩