Simplify your Express routing. Organize routes using file and directory structures, and let this tool auto-register them.
npm install express-router-fs --saveJust like in NextJS, your routes are declared based on their directory and file structure. Ensure each directory has an index file. In your main app file (where your Express app is initialized):
const app = express();
setupFileRouter(app, {
directory: 'routes',
extensions: ['.ts', '.js']
});routes/
|-- index.ts
|-- posts/
|-- |-- index.tsroutes/
|-- index.ts
|-- posts/
|-- |-- [postId]
|-- |-- |-- index.tsThe handler methods are declarated using exported functions, for example:
export function GET(request: Request, response: Response) {
console.log('Received GET request!')
}
export function POST(request: Request, response: Response) {
console.log('Received POST request!')
}