npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@fs-router/express

v0.0.9

Published

File-based routing for Express

Downloads

629

Readme

@fs-router/express

File-based routing for Express.js - brings Next.js-style routing conventions to Express with zero configuration.

npm version License: MIT

Features

  • 🚀 Zero Configuration
  • 📂 File-System Based Routes
  • 🛠️ Full TypeScript Support
  • ⚡ Lightweight & Fast
  • 🔌 Automatic Middleware Detection
  • 🎯 Express Native Routing

Installation

npm install @fs-router/express
# or
yarn add @fs-router/express
# or
pnpm add @fs-router/express

Quick Start

import express from 'express';
import { useFsRouter } from '@fs-router/express';

const app = express();
app.use(express.json());

await useFsRouter(app, {
  routesDir: 'routes', // Use 'src/routes' if using src folder
  verbose: true
});

app.listen(3000);

Route Conventions

File Structure to Routes

| File Path | Express Route | Description | |-----------|---------------|-------------| | route.ts | / | Root route | | users.route.ts | /users | Simple route | | users/[id].route.ts | /users/:id | Dynamic parameter | | posts/[...slug].route.ts | /posts/*slug | Catch-all route | | api/v1/users.route.ts | /api/v1/users | Nested route |

HTTP Method Handlers

Export functions named after HTTP methods:

// routes/users/[id].route.ts
import type { Request, Response } from 'express';

export const GET = (req: Request, res: Response) => {
  res.json({ id: req.params.id });
};

export const POST = (req: Request, res: Response) => {
  res.status(201).json({ created: true });
};

export const PUT = (req: Request, res: Response) => {
  res.json({ updated: true });
};

export const DELETE = (req: Request, res: Response) => {
  res.status(204).send();
};

// Fallback for other methods
export default (req: Request, res: Response) => {
  res.status(405).json({ error: `Method ${req.method} not allowed` });
};

Middleware Files

Create middleware with .middleware.ts suffix:

// routes/auth.middleware.ts
import type { Request, Response, NextFunction } from 'express';

export default (req: Request, res: Response, next: NextFunction) => {
  const token = req.headers.authorization;
  
  if (!token) {
    return res.status(401).json({ error: 'Unauthorized' });
  }
  
  // Validate token
  next();
};

Examples

REST API Structure

routes/
├── api/
│   ├── auth/
│   │   ├── login.route.ts          # POST /api/auth/login
│   │   ├── register.route.ts       # POST /api/auth/register
│   │   └── [...rest].middleware.ts # Middleware for /api/auth/* (all auth routes)
│   ├── users/
│   │   ├── route.ts                # GET, POST /api/users
│   │   └── [id].route.ts           # GET, PUT, DELETE /api/users/:id
│   └── protected.middleware.ts     # Middleware for /api/protected only
└── health.route.ts                 # GET /health

Async Handlers

// routes/posts.route.ts
export const GET = async (req, res) => {
  const posts = await db.posts.findMany();
  res.json({ posts });
};

export const POST = async (req, res) => {
  const post = await db.posts.create({ data: req.body });
  res.status(201).json({ post });
};

Error Handling

// routes/users/[id].route.ts
export const GET = async (req, res, next) => {
  try {
    const user = await db.users.findById(req.params.id);
    if (!user) return res.status(404).json({ error: 'Not found' });
    res.json({ user });
  } catch (error) {
    next(error);
  }
};

Catch-All Routes

// routes/docs/[...path].route.ts
export const GET = (req, res) => {
  const path = req.params.path || '';
  res.json({ 
    documentation: `Docs for ${path}` 
  });
};

API Reference

useFsRouter(app, options)

await useFsRouter(app, {
  routesDir: string;   // Required: Path to routes directory
  verbose?: boolean;   // Optional: Enable logging (default: false)
});

ExpressAdapter

For advanced usage with custom router instances:

import { ExpressAdapter } from '@fs-router/express';
import { createRouter } from '@fs-router/core';

const router = express.Router();
const adapter = new ExpressAdapter(router);

await createRouter(adapter, {
  routesDir: 'routes',
  verbose: true
});

app.use('/api', router);

Migration Guide

Before (Express Router)

app.get('/users', (req, res) => res.json({ users: [] }));
app.get('/users/:id', (req, res) => res.json({ id: req.params.id }));

After (FS Router)

// routes/users.route.ts
export const GET = (req, res) => res.json({ users: [] });

// routes/users/[id].route.ts
export const GET = (req, res) => res.json({ id: req.params.id });

Contributing

We welcome contributions! Please visit our GitHub repository to:

License

MIT © Universal FS Router