Routing

GraceJS offers a powerful filesystem-based routing system that automatically maps your file structure to URL paths. This method is preferred for its simplicity and ease of management.

Filesystem-Based Routing

To use filesystem-based routing, your route files should follow a specific naming pattern that reflects the HTTP method and the route path. For instance, a file named /v1/users/me/get.ts automatically registers a GET route at /v1/users/me.

Here's an example route file, get.ts:

import { createRoute } from '@grace-js/grace';

export default createRoute({
  handler: async (context) => {
    return {
      code: 200,
      body: { message: 'User profile information' },
    };
  },
});

Registering Routes

To register routes from a directory, use the registerRoutes method of the Grace instance:

// Assuming `app` is your Grace instance
app.registerRoutes(join(import.meta.dirname, 'routes'));

This method scans the specified directory and automatically registers all the routes.

Last updated