https://github.com/dobschal/express-route-loader
🪄 Auto load route handlers in Express JS applications.
https://github.com/dobschal/express-route-loader
auto-load autoloader express express-js expressjs node node-js nodejs router routes
Last synced: about 1 year ago
JSON representation
🪄 Auto load route handlers in Express JS applications.
- Host: GitHub
- URL: https://github.com/dobschal/express-route-loader
- Owner: dobschal
- Created: 2024-02-09T13:45:40.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-02T19:22:20.000Z (over 1 year ago)
- Last Synced: 2025-03-02T19:27:36.519Z (over 1 year ago)
- Topics: auto-load, autoloader, express, express-js, expressjs, node, node-js, nodejs, router, routes
- Language: JavaScript
- Homepage: https://github.com/dobschal/express-router-loader
- Size: 73.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Express Route Loader
Automatically load route handlers from script files, catch errors and map the function name to the URL path.
```javascript
function getArticles() {
} // becomes HTTP GET /articles 🙌
```

[](https://www.npmjs.com/package/@dobschal/express-route-loader)
## Get Started
### Installation
```bash
# Install the package via NPM:
npm install --save @dobschal/express-route-loader
```
### Import
```javascript
// ES6
import {routeLoader} from "@dobschal/express-route-loader";
// CommonJS
const {routeLoader} = require("@dobschal/express-route-loader");
```
### Use
```javascript
// Expect to have all route handlers in routes/
app.use(routeLoader(path.join(__dirname, "routes")));
```
## Router Handler
```javascript
// Will add GET /users route handler
export function getUsers() {
// ...
return users;
}
```
## Example
`app.js`
```javascript
import express from "express";
import path from "path";
import {routeLoader} from "@dobschal/express-route-loader";
// Instantiate express app and configure...
const app = express();
// Apply auto loader
app.use(routeLoader(path.join(__dirname, "routes")));
app.listen(/* ... */);
```
`routes/user.js`
```javascript
// This will apply as route handler for GET /users
export async function getUsers(body, req, res) {
const users = await loadUsers();
res.send({users});
}
```
## Auto Response
Instead of calling `res.send(/* ... */)` you can use the return keyword to send a JSON HTTP response.
```typescript
export async function getUsers() {
return {users: await loadUsers()};
}
```
## Error Handling
If an error is thrown inside a route handler, the error is automatically forwarded to
the [Express Error Handler](https://expressjs.com/en/guide/error-handling.html) via the NextFunction.
```javascript
export function postLogin({password}) {
// This will automatically be caught
// Just implement a global ExpressJS Error Handler
if (passwordIsWrong(password)) {
throw new Error("Password is wrong.");
}
}
```
## Prefixes
You can add a prefix to each route by exporting a `prefix` string variable.
```javascript
export const prefix = "/api/v1";
export function getUsers() { // Will be GET /api/v1/users
}
```