如何为用户创建自定义路由?以及如何为其添加挂钩?

How to make a custom route for users? And how to add hooks to it?

我正在尝试添加路由 /me 以获取用户身份验证信息。这就是我的文件。 我尝试在 users.services 文件中添加路由 /me,但出现此错误:"error: MethodNotAllowed: Method find is not supported by this endpoint."

我想通过用户对象(基于令牌)获得对 GET 方法的响应以路由“/me”。

users.service.js

// Initializes the `users` service on path `/users`
const createService = require('feathers-sequelize');
const createModel = require('../../models/users.model');
const hooks = require('./users.hooks');

module.exports = function (app) {
  const Model = createModel(app);
  const paginate = app.get('paginate');

  const options = {
    name: 'users',
    Model,
    paginate
  };

  // Initialize our service with any options it requires
  app.use('/users', createService(options));

  app.use('/me', {
      get(id, params) {
        return Promise.resolve([
          {
            id: 1,
            text: 'Message 1'
          }
        ])
      }
  })

  // Get our initialized service so that we can register hooks and filters
  const service = app.service('users');

  service.hooks(hooks);
};

users.hooks.js

const { authenticate } = require('@feathersjs/authentication').hooks;

const {
  hashPassword, protect
} = require('@feathersjs/authentication-local').hooks;

module.exports = {
  before: {
    all: [ ],
    find: [ authenticate('jwt') ],
    get: [],
    create: [ hashPassword() ],
    update: [ hashPassword() ],
    patch: [ hashPassword() ],
    remove: []
  },

  after: {
    all: [ 
      // Make sure the password field is never sent to the client
      // Always must be the last hook
      protect('password')
    ],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};

users.model.js

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {

    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    },


  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  users.associate = function (models) { // eslint-disable-line no-unused-vars
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
  };

  return users;
};

你做了什么

  app.use('/me', {
      get(id, params) {
        return Promise.resolve([
          {
            id: 1,
            text: 'Message 1'
          }
        ])
      }
  })

/me/:id 的实施路线。 find 方法是为 /me.

的基本路由运行的方法

不过我认为没有必要单独提供服务。一个更简单的解决方案是使用 before all 挂钩来更改 id 如果您正在访问 /users/me:

module.exports = function() {
  return async context => {
    if(context.id === 'me') {
      context.id = context.params.user._id;
    }
  }
}