Skip to content

Route Plugins

Route plugins allow you to add custom routes to SnowCMS, registered at /c/{route}.

Your route plugins should be in src/routes.

  • Directorysrc
    • Directoryroutes
      • routes.config.ts
      • example-plugin.ts
example-plugin.ts
import { defineRoutePlugin } from '@binaryfrost/snowcms/server';
export default defineRoutePlugin({
name: 'test-plugin',
plugin: ({ logger }) => (router) => {
logger.log('Registering routes');
router.get('/custom-route', (req, res) => {
handleAccessControl(res, req.user, 'USER');
res.json({
message: 'Only users with the USER role and higher can access this route'
});
});
// Every async route handler that can throw an error must be wrapped in asyncRouteFix(). Failure to do so will result in the whole server crashing if the route errors.
router.get('/users', asyncRouteFix(async (req, res) => {
const users = await db()<User>('users')
.select('id');
res.json({
message: 'You can also access the database in routes',
users
});
}));
}
});
hooks.config.ts
import { defineRoutePluginConfig } from '@binaryfrost/snowcms/server';
import examplePlugin from './example-plugin';
export default defineRoutePluginConfig({
plugins: [
examplePlugin
]
});