express-magic v1.0.1
express-magic
šŖ 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:
Absolute paths - Use a full system path
app.use(magic('/absolute/path/to/routes'))
Relative paths - Use
./
or../
notationapp.use(magic('./routes'))
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() {
const router = express.Router()
router.post('/', (req, res) => {
res.json({ message: 'Login endpoint' })
})
return router
}
Advanced Usage
Adding a Global Prefix
// Mount all routes with '/api' prefix
app.use(magic('routes', { prefix: '/api' }))
How It Works
Express Magic recursively scans the specified directory and:
- For directories: uses the directory name in the route path
- For files:
index.js
files are mounted at the current path level- Other
.js
files are mounted using their filename
- Supports both direct router exports and function-based exports
License
This project is licensed under the GPL-3.0 License - see the LICENSE file for details.
Author
Adam K Dean adamkdean@googlemail.com
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.