1

I am trying to set a route. I followed a tutorial. Everything is happening in a folder inside the api folder.

Route folder contains post.js file :

'use strict';

/**
 * post router.
 */

const { createCoreRouter } = require('@strapi/strapi').factories;

module.exports = createCoreRouter('api::post.post', {
    method: 'GET',
    path: '/api/posts/:id/comments',
    handler: 'posts.comments'
});

Controllers folder contains an other post.js file :

'use strict';

/**
 *  post controller
 */

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::post.post', ({strapi}) => ({
    comments: async (ctx) => {
        return "Hello"
    }
}));

Finally, when I tested URL : http://localhost:1337/api/posts/:id/comments; I have :

{
    "data": null,
    "error": {
        "status": 404,
        "name": "NotFoundError",
        "message": "Not Found",
        "details": {}
    }
}

What did I do wrong ? Is something missing ?

Emilie Tossan
  • 127
  • 1
  • 1
  • 16

1 Answers1

0

I think the url should be like http://localhost:1337/api/posts/1/comments and not http://localhost:1337/api/posts/:id/comments. Means url should provide actual id instead of ':id'. Try it.