Core Api Routes in plugin

System Information
  • Strapi Version: 4.7.1
  • Operating System:
  • Database: MySQL
  • Node Version: 16
  • NPM Version:
  • Yarn Version:

Hello everyone,

I am working on a custom plugin and I’d like to create a complete entity there (with content-type, routes, controller and service). The problem is that I don’t understand how to include the core api routes. I have a file where I generate them:

# file src/plugins/my-plugin/server/routes/my-entity.js

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

module.exports = createCoreRouter('plugin::my-plugin.my-entity');

but then I’m not sure how to do, I have tried several approaches, for example:

# file src/plugins/my-plugin/server/routes/index.js

const myEntityRoutes = require('./my-entity');

module.exports = {
  'content-api': {
    type: 'content-api',
    routes: myEntityRoutes.routes
  },
};

Anyway, every time I got the following error:

strapi_1  | [2023-03-07 17:33:22.650] debug: ⛔️ Server wasn't able to start properly.
strapi_1  | [2023-03-07 17:33:22.652] error: Cannot read properties of undefined (reading 'kind')
strapi_1  | TypeError: Cannot read properties of undefined (reading 'kind')
strapi_1  |     at isSingleType (/opt/node_modules/@strapi/utils/lib/content-types.js:90:25)
strapi_1  |     at createRoutes (/opt/node_modules/@strapi/strapi/lib/core-api/routes/index.js:6:7)
strapi_1  |     at Object.get routes [as routes] (/opt/node_modules/@strapi/strapi/lib/factories.js:59:31)
strapi_1  |     at /opt/node_modules/lodash/lodash.min.js:54:69
strapi_1  |     at Ke (/opt/node_modules/lodash/lodash.min.js:41:281)
strapi_1  |     at Ve (/opt/node_modules/lodash/lodash.min.js:42:206)
strapi_1  |     at /opt/node_modules/lodash/lodash.min.js:41:323
strapi_1  |     at /opt/node_modules/lodash/lodash.min.js:54:66
strapi_1  |     at Ke (/opt/node_modules/lodash/lodash.min.js:41:281)
strapi_1  |     at Ve (/opt/node_modules/lodash/lodash.min.js:42:206)
traveleco-platform-backend_strapi_1 exited with code 1

Is it possible to use createCoreRouter in a plugin or do I have to create every single route manually? Can anyone help me?

Thank you in advance

createCoreRouter is not yet suported in plugins will most likely be fixed in v5

1 Like

Ok thank you

Thanks for this info!

I suggest to put this into the documentation and will save hours of debugging for others too :slight_smile:

3 Likes