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-tang

v1.0.3

Published

Convention-based automatic Express application route discovery and mounting

Downloads

6

Readme

express-tang

Convention-based automatic Express application route discovery and mounting

$ npm install express-tang --save

express-tang automatically discovers JavaScript files containing your routes and mounts them to your Express application.

Usage

// instead of using multiple use statements
app.use('/', './routes/index.js');
app.use('/api/books', './routes/books.js');
app.use('/api/users', './routes/users.js');

// You can just do this
tang.register(app);

BUT you must follow the convention for this to work!

Conventions

You can leave your routes where you see fit in your application directory, because express-tang doesn't care. Instead, it relies on a few simple naming conventions that you need to follow for the files that contain your routes.

Default Conventions

  • Any text file ending in .route.js or .routes.js will be considered a route
  • Any dashes (-) found in the file name will be replaced with camel casing
  • Any dots (.) found in the file name will be replace with a forward slash
  • Files prefixed with index will be mounted at /

Examples

Given the following file structure

└── someFolder
    ├── index.routes.js
    ├── api.books-in-library.routes.js
    └── api.users.routes.js

Using:

tang.register(app);

Would be equivalent to:

app.use('/', 'someFolder/index.routes.js');
app.use('/api/booksInLibrary', 'someFolder/api.books-in-library.routes.js');
app.use('/api/users', 'someFolder/api.users.routes.js');

The more route files you have, the bigger the benefit you get from using express-tang.

Configuration

To maximize your mileage and satisfaction, the default values used by express-tang can be overridden by either setting the properties directly, or via the setter methods.

| Property | Setter Method | Default Value | |--------------|---------------------------|---------------------| | FileNameMask | setFileNameMask(RegExp) | /\.routes?\.js$/i | | RoutesDir | setRoutesDir(Path) | application root | | SearchType | setSearchType(string) | breadthFirst |

File Name Mask

Regular Expression Object

What, you don't like my file name convention? Make your own! This property is the regular expression used to match files that should be included as routes. Just remember that the file name mask you use will be removed from the file name before converting it to a path.

tang.FileNameMask = /\.feature.route\.js$/i;
tang.setFileNameMax(/\.feature.route\.js$/i);

Routes Root Directory

Any valid folder accessible to the application

By default, express-tang will use require.main.filename to determine where to start searching for matching files. You can provide a path if this default doesn't meet your needs.

tang.RoutesDir = 'path/to/app/routes';
tang.setRoutesDir('path/to/app/routes');

Search Type

Must be either breadthFirst or depthFirst.

Since the routes will be added in the order they are found, you might want to control how express-tang searches for them. By default, it will use a breadth-first approach, which means the files found higher in the tree structure will be added first - even if they are in different branches. You can change it to a depth-first search if that better suits your needs.

tang.SearchType = 'depthFirst';
tang.setSearchType('depthFirst');

Chainable Methods

All methods are chainable.

tang.setSearchType('depthFirst').setRoutesDir('../app/features').register(app);

Exceptions

Exceptions will be thrown if the values provided are invalid.

tang.setSearchType('widthFirst');                       // will throw an exception
tang.setRoutesDir('../invalid/or/inaccessible/folder'); // will throw an exception
tang.setFileNameMask("Not a regular expression");       // will throw an exception

Think you can get around this by setting the properties directly? Think again!

tang.SearchType = 'widthFirst';
tang.RoutesDir = '../invalid/or/inaccessible/folder';
tang.FileNameMask = "Not a regular expression";
tang.register(app); // will throw an exception

An exception will also be thrown if a valid express application is not provided.

tang.register(); // will throw an exception

Notes

Your application file structure is intentionally traversed synchronously, because you don't want the application to continue starting up until all the routes have been added. This might cause your application to take longer to start up, but not by a noticeable amount of time.