Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

dirent-router

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dirent-router

Generate routes from a directory

latest
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

dirent-router

Generate routes from a directory

Install

npm install dirent-router
# or
pnpm install dirent-router

Route Directory Structure

Each .ts file in your routes directory becomes an endpoint. Exported function names correspond to HTTP methods (get, post, put, delete, etc.).

  • index.ts files map to the parent directory path
  • Other .ts files map to a path segment matching their filename
  • Directory names wrapped in brackets (e.g., [id]) become route variables

Example:

routes/
├── index.ts              →  /
├── companies/
│   ├── index.ts          →  /companies
│   ├── search.ts         →  /companies/search
│   └── [id]/
│       └── index.ts      →  /companies/[id]
// routes/index.ts
export const get = () => {
  return 'foo';
}

// routes/companies/index.ts
export const get = () => {
  return [{ id: 'a' }, { id: 'b' }];
}

export const post = () => {
  return { ok: true };
}

// routes/companies/search.ts
export const get = (searchQuery: string) => {
  return { searchQuery };
}

// routes/companies/[id]/index.ts
export const get = (id: string) => {
  return { id };
}

Usage

Integration

import fs from "fs";
import { getRoutes } from "dirent-router";

const routes = await getRoutes({
  routesDir: "./path/to/your/routes",
});

console.log(
  JSON.stringify(
    routes,
    (_, v) => (typeof v === "function" ? `${v.name}(...)` : v),
    2,
  ),
);

// Output example
[
  {
    "filepath": "sample-routes/companies/[id]",
    "relativepath": "companies/[id]",
    "endpoint": "/companies/[id]",
    "args": [
      {
        "type": "path",
        "value": "companies"
      },
      {
        "type": "variable",
        "value": "id"
      }
    ],
    "methods": {
      get: "[Function]"
    }
  }
]

CLI

dirent-router ./path/to/your/routes

FAQs

Package last updated on 15 Feb 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts