Help me to understand how to create a route please

Hello,

I am trying to set a route. I am following 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 ?

change your post.js to sth like this

module.exports = {
    routes: [
        { // Path defined with a URL parameter
            method: 'GET',
            path: '/location/top',
            handler: 'controller.findTopLocation',
            config: {
                auth: false,
            },
        },
        { // Path defined with a URL parameter
            method: 'GET',
            path: '/theme/top',
            handler: 'controller.findTopTheme',
            config: {
                auth: false,
            },
        },
        { // Path defined with a URL parameter
            method: 'GET',
            path: '/theme/find',
            handler: 'controller.findThemeByName',
            config: {
                auth: false,
            },
        }
    ]
}


Then in the controller folder make another file and call it controller.js with something like this

module.exports = ({strapi})=>{

  return {

    async findTopLocation(ctx, next) { // GET api/location/top

      try {

        let limit = ctx.query.limit 

          ctx.body -> use this to return data

      } catch (err) {

        return ctx.internalServerError('Error',err) ;

      }

    },

    async findTopTheme(ctx, next) {

    },

    async findThemeByName(ctx, next) {

    }

  }

};

Strapi is bugging… I don’t understand what I have write instead of “use this to return data” : “->” is not accepted. Also, “next” and “limit” are not used.

1 Like

Those are comments and examples I added to how to handle incoming requests.
Use this instead of those lines


 ctx.body ={ test:123}

Ok, thank you. I understand better. :slight_smile: I didn’t get the right result but at least my app didn’t crash.

This is the code I have in post.js file inside routes folder. Is it correct ?

module.exports = {
  routes: [
      { // Path defined with a URL parameter
          method: 'GET',
          path: '/api/posts/:id/comments',
          handler: 'post.comments',
          config: {
              auth: false,
          },
      }
  ]
}

This file looks fine. Keep in mind that your endpoint will be
api/api/posts/:id/comments

Also I guess we already have a commemts plugin in strapi marketplace. So maybe it has conflict with it.
Also post.comment controller should return something.

Also make sure to check your contoller’s access level in the settings, under user permissions. Make it public to be able to access it without logging in