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

express-magic

v1.1.1

Published

Automagically load Express routes

Downloads

42

Readme

express-magic

npm version License: GPL-3.0 npm downloads GitHub Issues

🪄 Automagically load and mount Express routes with zero configuration

Express Magic is a lightweight, zero-configuration route loader for Express.js that automatically discovers and mounts your route files using a simple, convention-based approach. Stop manually registering routes and let the magic happen!

Features

  • 🚀 Zero configuration required
  • 📁 Automatic route discovery and mounting
  • 🌳 Support for nested directory structures
  • 🎯 Optional route prefixing
  • 💪 TypeScript friendly
  • 🪶 Lightweight with no dependencies

Installation

npm install express-magic

Quick Start

import express from 'express'
import magic from 'express-magic'

const app = express()

// Mount all routes from the 'routes' directory
app.use(magic('routes'))

app.listen(3000, () => {
  console.log('Server running on port 3000')
})

Path Resolution

Express Magic intelligently resolves your routes directory using three methods:

  1. Absolute paths - Use a full system path

    app.use(magic('/absolute/path/to/routes'))
  2. Relative paths - Use ./ or ../ notation

    app.use(magic('./routes'))
  3. Named paths - Just use the directory name

    app.use(magic('routes'))

When using a named path, Express Magic automatically resolves it relative to the file that calls it, not the current working directory. For example:

project/
├── src/
│   ├── index.js     # Your server file
│   └── routes/      # Your routes directory
│       └── users.js
└── package.json

In src/index.js, you can simply use:

app.use(magic('routes'))

Express Magic will automatically find the routes directory next to your index.js file. No need to use src/routes or worry about the current working directory!

Directory Structure

Express Magic follows a simple convention for route mounting. Here's an example structure:

server.js
routes/
├── index.js         # mounted at /
├── users.js         # mounted at /users
├── auth/
│   ├── index.js     # mounted at /auth
│   ├── login.js     # mounted at /auth/login
│   └── register.js  # mounted at /auth/register
└── api/
    └── v1/
        ├── users.js # mounted at /api/v1/users
        └── posts.js # mounted at /api/v1/posts

Route File Examples

Simple Route File (users.js)

import express from 'express'

const router = express.Router()

router.get('/', (req, res) => {
  res.json({ message: 'Users endpoint' })
})

export default router

Function-Based Route File (auth/login.js)

import express from 'express'

export default function(context) {
  const router = express.Router()

  router.post('/', (req, res) => {
    res.json({ message: 'Login endpoint', context })
  })

  return router
}

Advanced Usage

Adding a Global Prefix

// Mount all routes with '/api' prefix
app.use(magic('routes', { prefix: '/api' }))

Injecting Context into Routes

You can pass additional properties in the options object that will be injected into function-based route modules:

// Pass context to route functions
app.use(magic('routes', {
  prefix: '/api',
  authService: myAuthService,
  config: { maxUploadSize: '10mb' }
}))

Then in your route file:

export default function({ authService, config }) {
  const router = express.Router()

  router.post('/upload', (req, res) => {
    // Use injected context
    res.json({
      message: 'Upload endpoint',
      maxSize: config.maxUploadSize
    })
  })

  return router
}

How It Works

Express Magic recursively scans the specified directory and:

  1. For directories: uses the directory name in the route path
  2. For files:
    • index.js files are mounted at the current path level
    • Other .js files are mounted using their filename
  3. Supports both direct router exports and function-based exports
  4. Passes any additional options as context to function-based route modules

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

Author

Adam K Dean [email protected]

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.