Strapi / Nuxt - 找不到自定义路线

Strapi / Nuxt - Can't find custom route

我用它在 strapi 和 nuxt 中设置身份验证: Auth with Strapi and Nuxt

我目前正在尝试检索特定于经过身份验证的用户的项目(已经签出此 )。为此,我在 Strapi (/api/routine/config/routes.json) 中创建了一条自定义路线:

{
  "method": "GET",
  "path": "/routines/me",
  "handler": "Routine.me",
  "config": {
    "policies": []
  }
}

和自定义控制器 (/api/controllers/Routine.js):

module.exports = {
  me: async (ctx) => {
    const user = ctx.state.user;
    if (!user) {
      return ctx.badRequest(null, [{ messages: [{ id: 'No authorization header was found' }] }]);
    }

    const data = await strapi.services.routine.find({user:user.id});  

    if(!data){
      return ctx.notFound();
    }

    ctx.send(data);
  },
};

我已经通过 Strapi 管理员授予经过身份验证的用户访问 'me' 的权限。当我从 Nuxt 到达终点时:

const routines = await axios.get(http://localhost:1337/routines/me)

我收到这个错误:

GET http://localhost:1337/routines/me 404 (Not Found)

为什么找不到自定义路由?我使用了错误的端点吗?

也许你已经解决了,但你好像忘了在请求中发送认证头。

    const routines = await axios.get(
        'http://localhost:1337/routines/me', {
            headers: {
                Authorization:
                this.$auth.getToken('local'),
            },
        }

这是我的 Strapi 路由配置中的错误。答案是通过非常有用的 Strapi 论坛提供的: 403 forbidden when calling custom controller from Nuxt

这是问题所在:

{
  "method": "GET",
  "path": "/routines/:id",
  "handler": "routine.findOne",
  "config": {
    "policies": []
  }
},
{
  "method": "GET",
  "path": "/routines/me",
  "handler": "routine.me",
  "config": {
    "policies": []
  }

所以基本上你现在正在走第一条路线,它假设 我实际上是一个 :id。 Koa 正在使用正则表达式进行验证,因此在这种情况下它采用第一个匹配的路由。将带有 /me 的路线移动到带有 /:id

的路线上方