express-router-fs
v1.2.0
Published
Simplify your Express routing. Organize routes using file and directory structures, and let this tool auto-register them.
Downloads
3
Readme
Express File-based Router (NextJS style)
Simplify your Express routing. Organize routes using file and directory structures, and let this tool auto-register them.
Installation
npm install express-router-fs --save
Quick Start
Just 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']
});
Directory example:
Simple example:
routes/
|-- index.ts
|-- posts/
|-- |-- index.ts
You can also use Slugs (> v1.2.0)!
routes/
|-- index.ts
|-- posts/
|-- |-- [postId]
|-- |-- |-- index.ts
The 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!')
}