您是否使用 CLI 创建应用程序和服务?也许会更容易。但实际上并不是必须为每个服务都创建一个数据库,除非真的需要使用超过 1 个。
关于逻辑代码,这取决于。它可以作为express middleware 或service hooks (for every service or for the application) 处理。第二种情况更多的是关于预处理和后处理客户端请求。当然,对自身进行的操作可以是一个类、一个函数,甚至是一个 API 调用。例如,您可以仅在执行“get”操作之前检查客户端在请求中发送的参数,将 ckeck 添加到 GET before hook 中。还有一个钩子来检查身份验证。
//in other some place
module.exports = (hook) => {
return hook.params.query.device_type === 'smartphone'
? console.log('is a smartphone request')
: console.log('is not a smartphone request');
}
//src/services/devices/devices.hooks.js
const { authenticate } = require('@feathersjs/authentication').hooks;
const myVerification = require('path/to/myverification');
const myVerification2 = require('path/to/myverification2');
module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [myVerification, myVerification2],
create: [],
update: [],
patch: [],
remove: []
},
after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},
error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};