Simple API Router is a lightweight npm package that simplifies setting up Express APIs. It automatically detects all API routes in a defined directory and binds them to an Express app. Additionally, it provides an API client generator for frontend integration and automatic API documentation generation.
To install the package in an existing project, use the following command:
npm install @notnexx/n-sar
Create an Express app and use @notnexx/n-sar
to automatically load all routes from the api/
directory:
const express = require('express');
const loadRoutes = require('@notnexx/n-sar');
const app = express();
const PORT = 3000;
app.use(express.json());
loadRoutes(app);
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
The package allows easy creation of a sample project with a predefined structure:
npx n-sar create my-api-project
This creates a new folder my-api-project
with the following structure:
my-api-project/
βββ api/
β βββ router.js
β βββ user.js
βββ package.json
βββ server.js
Then, start the project:
cd my-api-project
npm install
node server.js
With n-sar
, you can automatically generate a frontend API client that connects to your backend (localhost or your server domain/IP):
npx n-sar export-routes http://localhost:3000
This creates an apiClient.js
file with functions for each detected route, making API calls simple:
import APIClient from './apiClient';
APIClient.user_get().then(response => console.log(response));
You can automatically generate API documentation based on your routes:
npx n-sar generate-docs
This will create an API_DOCS.md
file containing all detected API endpoints and their methods.
The package expects API files to be located in the api/
directory. Each file corresponds to a route. Example:
const express = require('express');
const router = express.Router();
router.get('/', (req, res) => {
res.json({ message: 'User endpoint' });
});
module.exports = router;
This automatically creates the route:
GET /user
This package is licensed under the MIT License.
If you find Simple API Router useful, consider sponsoring to help maintain and improve it!
Created by NotNexx.