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

express-advanced-router

v1.0.5

Published

File-based router for express

Downloads

5

Readme

express-advanced-router

This module provides a robust file-based routing solution for Express.js applications. It supports a wide range of routing methods, including public and catch-all routes, with various syntax options for flexible and comprehensive route management.

You can define route types (Dynamic, Static, Catch-All, Public) by using specific syntax structures within folder names. The same flexibility applies to methods within routes. This approach helps you avoid complexity and provides a more efficient and advanced routing system.

How to use

To use this application, first create an Express application. Pass the application as a parameter to the module exported as the default from "express-advanced-router". If a RouteOptions object is not provided, it will search for the routes folder in "src/routes". If the folder is not found, an error will be thrown.

npm i express-advanced-router
import express from "express";
import advancedRouter from "express-advanced-router";

const app = express();

await advancedRouter(app);

app.listen(3000);

Route Options

You can use customizable settings to change the location of the routes folder, the base route (e.g., baseRoute), or the public route name. To do this, create a RouteOptions object and pass it as a parameter after your expressApp.

If these settings are not configured, the following default values will be used:

| Option | Default | |---------------|-----------| | routesPath | "src/routes" | | publicRouteName | "/public" | | baseRoute | "/" |

routes_options

Route Types & Prefixes

When defining route types, specific naming conventions should be followed. These conventions are straightforward and easy to understand. For static routes, no special syntax is required—simply name the route as needed.

| Route Type | Description | RegExp | |----------------|---------------------------------------------------------------------------------------------|-----------------------| | PUBLIC | The public route should be placed in the root directory of the routes folder. It must be a directory and should be named _public. | /^_public$/i | | STATIC | For static routes, simply name the file as the desired route. | | | DYNAMIC | To define a dynamic route, append [[]] to the folder name and place the parameter name between [[ and ]]. | /^\[\[[0-9a-zA-Z]+\]\]/ | | CATCH_ALL| To define a catch-all route, simply prefix the folder name with $. | /^\$/ |

routes_named dynamic_router_named

Route Ordering

Routes are ordered based on their type, not by file order. This approach helps prevent inconsistencies and errors.

  • The order of precedence is as follows: Public > Static > Dynamic > Catch-All.

routes_short

Route Handlers

To define handlers within a route, you should name the file using the handler method's name. This allows you to specify the HTTP method that the file will handle. Please note the following conventions: The method name should be written in uppercase and followed by an underscore (_). After that, you can use any naming convention you prefer. For example: GET_index.ts. Both .ts and .js file extensions are supported.

Currently supported methods:

  • GET = GET_
  • POST = POST_
  • PUT = PUT_
  • DELETE = DELETE_
  • PATCH = PATCH_
  • OPTIONS = OPTIONS_
  • HEAD = HEAD_

routes_handler

Handler File Structure

In the file structure for handlers, there should be a handler function that is exported as the default, along with an array that contains any middleware. Important points to note: The handler must always be exported as the default, while middleware should be exported as a named export within an array named middlewares.

Middlewares

Middlewares are similar to handlers in that they also take req, res, and next as parameters. To apply middlewares in a handler, import them into the handler's file. Then, define a middlewares variable as an array of type Handler[]. Place the middlewares you wish to apply in the array in the desired order, and export this array as a named export.

Example Handler With Middlewares

The handler must always be exported as the default.

import type { Handler } from "express";
import {mw_1,mw_2} from "../middlewares/index";

export const middlewares: Handler[] = [mw_1, mw_2];

const handler: Handler = async (req, res) => {
  res.sendStatus(200);
};

export default handler;