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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@metrobuzz/express-routes-loader

v0.0.1

Published

This module provides a flexible way to load and register routes in an Express application from a specified folder.

Downloads

78

Readme

Route Loader for Express

This module provides a flexible way to load and register routes in an Express application from a specified folder. It supports customizable logging and allows for the inclusion of a custom handler for wildcard routes.

Overview

The route loader dynamically imports route files from a specified folder, registers them with an Express app, and handles wildcard routes. It provides options to customize logging and define a handler for unmatched routes.

Function Signature

export default async (
  routeFolderName: string,
  app: Express,
  servicePrefix?: string,
  wildcardHandler?: (req: Request, res: Response, next: NextFunction) => void,
) => { ... };

Parameters

  • routeFolderName: The path to the folder containing route files. Each file should export an array of RouteHandler objects.
  • app: The Express application instance where routes will be registered.
  • servicePrefix (optional): A prefix to prepend to all route paths.
  • wildcardHandler (optional): A custom handler function for wildcard routes (i.e., routes that do not match any registered routes).

How It Works

  1. Validation: Checks if the provided route folder path is valid. If not, an error is thrown.
  2. Route File Import: Reads all files in the route folder, strips the index file and file extensions, and imports each file.
  3. Route Registration: For each route file, validates that it exports an array of RouteHandler objects. Each route is registered with the Express app. Duplicate routes are logged as warnings.
  4. Wildcard Route Handling: A default wildcard route handler is registered to handle any requests that do not match registered routes. If a wildcardHandler is provided, it will be used instead of the default handler.
  5. Logging: Logs the loading process and route registrations. The logging behavior can be customized.

Example Usage

import express from "express";
import routeLoader from "@metrobuzz/express-routes-loader";

const app = express();
const routeFolder = "./routes";

// Custom handler for wildcard routes
const customWildcardHandler = (req, res) => {
  res.status(404).json({ message: "Custom Not Found" });
};

// Load routes with a custom prefix and wildcard handler
routeLoader(routeFolder, app, "/api", customWildcardHandler)
  .then(() => {
    app.listen(3000, () => console.log("Server running on port 3000"));
  })
  .catch((err) => console.error(err));

Alternative,

import express from "express";
import routeLoader from "@metrobuzz/express-routes-loader";

const app = express();
const routeFolder = "./routes";

// Custom handler for wildcard routes
const customWildcardHandler = (req, res) => {
  res.status(404).json({ message: "Custom Not Found" });
};

const load = async () => {
  try {
    await routeLoader(routeFolder, app, "/api", customWildcardHandler);
    app.listen(3000, () => console.log("Server running on port 3000"));
  } catch (error) {
    console.error("Error loading routes:", error);
  }
};

load();

Route File Format

Each file in the routeFolderName should export an array of RouteHandler objects. Here's a sample structure:

Folder Structure

routes/
  ├── users.ts
  ├── products.ts
  └── orders.ts

Sample Route Files

routes/users.ts

import { Request, Response } from "express";
import { RouteHandler } from "@metrobuzz/express-routes-loader";

// Define route handlers
const getUsers: RouteHandler = {
  path: "/users",
  method: "get",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "List of users" });
    },
  ],
};

const createUser: RouteHandler = {
  path: "/users",
  method: "post",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "User created" });
    },
  ],
};

// Export route handlers as an array
export default [getUsers, createUser];

routes/products.ts

import { Request, Response } from "express";
import { RouteHandler } from "@types";

// Define route handlers
const getProducts: RouteHandler = {
  path: "/products",
  method: "get",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "List of products" });
    },
  ],
};

const createProduct: RouteHandler = {
  path: "/products",
  method: "post",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "Product created" });
    },
  ],
};

// Export route handlers as an array
export default [getProducts, createProduct];

routes/orders.ts

import { Request, Response } from "express";
import { RouteHandler } from "@types";

// Define route handlers
const getOrders: RouteHandler = {
  path: "/orders",
  method: "get",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "List of orders" });
    },
  ],
};

const createOrder: RouteHandler = {
  path: "/orders",
  method: "post",
  handlers: [
    (req: Request, res: Response) => {
      res.json({ message: "Order created" });
    },
  ],
};

// Export route handlers as an array
export default [getOrders, createOrder];

Alternative Route Handler Format

Route handlers can also be defined in an alternative format. Here's an example:

import { Request, Response } from "express";
import { RouteHandler } from "@types";

// Define route handlers
const serviceLoader: RouteHandler[] = [
  {
    path: "/example",
    method: "get", // get, put, post, patch, delete
    handlers: [
      (req: Request, res: Response) => {
        res.json({ message: "Example route" });
      },
    ],
  },
];

export default serviceLoader;

Explanation

  1. File Structure: Each file in the routes folder represents a module containing route handlers. These files are imported and their default export is expected to be an array of RouteHandler objects.

  2. RouteHandler Interface: Each RouteHandler object includes:

    • path: The route path.
    • method: The HTTP method (e.g., 'get', 'post').
    • handlers: An array of handler functions that handle requests to this route.
  3. Export: Each file exports an array of RouteHandler objects. This array is dynamically loaded and registered by the route loader function.

This setup allows you to modularize your routes and manage them easily. Each route file is responsible for its own set of routes, making the route configuration clean and maintainable.