Search the Community
Showing results for tags 'routing'.
-
I'm running a Fastify application on Johnny and have structured my project to keep routes separate for maintainability. Instead of defining all routes in app.js, I have a routes/ folder where each route is stored in its own file. For example, I have routes/apps.js, which looks like this: const knexConfig = require('../knexfile'); const knex = require('knex')(knexConfig.production); const authMiddleware = require('../middlewares/auth'); async function appRoutes(fastify, opts) { fastify.post('/addapps', { preHandler: authMiddleware }, async (request, reply) => { ... } module.exports = appRoutes; In the main app.js, I have this: const fastify = require('fastify')({ logger: true }); async function main() { try { await fastify.register(require('./routes/apps')); fastify.get('/', async (req, reply) => { return { message: 'Ok!' }; }); await fastify.listen({ port: 3000, host: '127.0.0.1' }); } catch (err) { fastify.log.error(err); process.exit(1); } } main(); The issue is that while the / route in app.js works just fine, any additional routes I place in routes/ do not work unless I manually define them inside app.js. I get the "not found" error. { "message": "Route GET:/addapps not found", "error": "Not Found", "statusCode": 404 } This defeats the purpose of splitting routes into separate files for maintainability. Is there a limitation on Johnny preventing Fastify from loading external routes properly or is it a config issue on my path? How can I ensure all routes inside routes/ are correctly recognized when using fastify.register()? I've attached a screenshot of my config. Thanks!
