Jump to content

Recommended Posts

Posted (edited)

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! 

 

Screenshot_20250321-153930.png

Edited by ujcis
Added the error message
Posted

I'm not sure if anyone here will know this one honestly. I've never even heard of Fastify until now, and have near-zero experience with Node.

Krydos would be the one who would know if anybody will. I wouldn't be surprised if this is to do with Passenger or how node works here. I assume this code works if you run it locally on your PC?

Escalating.

Posted
9 hours ago, ujcis said:
const knexConfig = require('../knexfile'); 

Try using absolute paths.

  • Krydos locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...