1. Database Connection Errors

Understanding the Issue

AdonisJS applications may fail to connect to the database, preventing migrations, queries, and application startup.

Root Causes

  • Incorrect database credentials in the .env file.
  • Database server not running or inaccessible.
  • Missing database driver dependencies.

Fix

Ensure that database credentials are correctly set in .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_USER=root
DB_PASSWORD=yourpassword
DB_DATABASE=adonis

Verify that the database server is running:

mysql -u root -p -e "SHOW DATABASES;"

Ensure the correct database driver is installed:

npm install mysql2

2. Middleware Not Executing

Understanding the Issue

Custom middleware may not be executed, resulting in unexpected behavior such as missing authentication checks or unprocessed requests.

Root Causes

  • Middleware not registered in start/kernel.ts.
  • Incorrect middleware execution order.

Fix

Register middleware in start/kernel.ts:

Server.middleware.register([
  () => import('@ioc:Adonis/Core/BodyParser'),
  () => import('@ioc:Adonis/Auth'),
]);

Ensure global middleware is correctly structured:

Server.middleware.registerNamed({
  auth: () => import('App/Middleware/Auth'),
});

3. Authentication Not Working

Understanding the Issue

Users may not be able to log in or access protected routes due to authentication errors.

Root Causes

  • Incorrect authentication provider configuration.
  • Missing authentication guard setup.
  • Invalid JWT or session handling issues.

Fix

Ensure authentication is properly configured in config/auth.ts:

const authConfig: AuthConfig = {
  guard: 'api',
  guards: {
    api: {
      driver: 'jwt',
      tokenProvider: {
        driver: 'database',
        table: 'api_tokens',
      },
      persistUser: false,
      model: () => import('App/Models/User'),
    },
  },
};

Ensure the authentication middleware is applied to protected routes:

Route.group(() => {
  Route.get('profile', 'UsersController.profile');
}).middleware('auth');

4. CORS Issues Preventing API Calls

Understanding the Issue

Cross-Origin Resource Sharing (CORS) errors may prevent front-end applications from accessing AdonisJS APIs.

Root Causes

  • CORS settings not properly configured.
  • Browser security policies blocking requests.

Fix

Enable CORS in config/cors.ts:

const corsConfig: CorsConfig = {
  enabled: true,
  origin: true,
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
};

Restart the AdonisJS server after making changes:

node ace serve --watch

5. High Memory or CPU Usage

Understanding the Issue

AdonisJS applications may consume excessive memory or CPU resources, leading to slow performance and degraded responsiveness.

Root Causes

  • Unoptimized database queries.
  • Memory leaks due to large object allocations.

Fix

Use pagination to limit data queries:

const users = await User.query().paginate(1, 10);

Monitor memory usage and optimize garbage collection:

node --expose-gc app.js
global.gc();

Conclusion

AdonisJS is a powerful back-end framework, but troubleshooting database connectivity, middleware execution, authentication failures, CORS issues, and performance bottlenecks is crucial for maintaining a stable application. By following best practices in configuration management, security enforcement, and API optimization, developers can ensure reliable and scalable AdonisJS applications.

FAQs

1. Why is my AdonisJS application failing to connect to the database?

Ensure that the database credentials are correctly set in .env, the database server is running, and the required database driver is installed.

2. How do I fix middleware not executing in AdonisJS?

Check that middleware is registered in start/kernel.ts and ensure the correct execution order.

3. Why is authentication not working in AdonisJS?

Verify that the authentication provider is correctly configured in config/auth.ts and that routes are protected with the proper middleware.

4. How do I resolve CORS errors in AdonisJS?

Ensure that CORS settings are properly enabled in config/cors.ts and restart the server to apply changes.

5. How can I reduce high memory usage in AdonisJS?

Optimize database queries with pagination, monitor memory leaks, and use garbage collection to free up unused memory.