Skip to content

Commit 301fe1f

Browse files
committed
init
1 parent 2ef964d commit 301fe1f

11 files changed

+5489
-0
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Auto detect text files and perform LF normalization
22
* text=auto
3+
/example

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
/example

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 NotNexx
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Simple API Router
2+
3+
## Overview
4+
**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.
5+
6+
## Installation
7+
8+
To install the package in an existing project, use the following command:
9+
10+
```bash
11+
npm install simple-api-router
12+
```
13+
14+
## Usage
15+
16+
### 1. Manual Setup
17+
18+
Create an Express app and use `simple-api-router` to automatically load all routes from the `api/` directory:
19+
20+
```javascript
21+
const express = require('express');
22+
const loadRoutes = require('simple-api-router');
23+
24+
const app = express();
25+
const PORT = 3000;
26+
27+
app.use(express.json());
28+
loadRoutes(app);
29+
30+
app.listen(PORT, () => {
31+
console.log(`Server running at http://localhost:${PORT}`);
32+
});
33+
```
34+
35+
### 2. Automatically Generate a Sample Project
36+
37+
The package allows easy creation of a sample project with a predefined structure:
38+
39+
```bash
40+
npx sar my-api-project
41+
```
42+
43+
This creates a new folder `my-api-project` with the following structure:
44+
45+
```
46+
my-api-project/
47+
├── api/
48+
│ ├── router.js
49+
│ └── user.js
50+
├── package.json
51+
└── server.js
52+
```
53+
54+
Then, start the project:
55+
56+
```bash
57+
cd my-api-project
58+
npm install
59+
node server.js
60+
```
61+
62+
## API Definitions
63+
64+
The package expects API files to be located in the `api/` directory. Each file corresponds to a route. Example:
65+
66+
### `api/user.js`
67+
68+
```javascript
69+
const express = require('express');
70+
const router = express.Router();
71+
72+
router.get('/', (req, res) => {
73+
res.json({ message: 'User endpoint' });
74+
});
75+
76+
module.exports = router;
77+
```
78+
79+
This automatically creates the route:
80+
```
81+
GET /user
82+
```
83+
84+
## License
85+
This package is licensed under the **MIT License**.
86+
87+
## Author
88+
Created by NotNexx.
89+

bin/cli.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const projectName = process.argv[2] || 'my-api-project';
7+
const projectPath = path.join(process.cwd(), projectName);
8+
9+
const files = {
10+
'package.json': `{
11+
"name": "${projectName}",
12+
"version": "1.0.0",
13+
"main": "server.js",
14+
"dependencies": {
15+
"express": "latest"
16+
}
17+
}`,
18+
'server.js': `const express = require('express');
19+
const loadRoutes = require('./api/router');
20+
21+
const app = express();
22+
const PORT = 3000;
23+
24+
app.use(express.json());
25+
loadRoutes(app);
26+
27+
app.listen(PORT, () => {
28+
console.log('Server running on http://localhost:' + PORT);
29+
});`,
30+
'api/router.js': `const fs = require('fs');
31+
const path = require('path');
32+
33+
module.exports = (app) => {
34+
const apiDir = path.join(__dirname);
35+
fs.readdirSync(apiDir).forEach(file => {
36+
if (file !== 'router.js' && file.endsWith('.js')) {
37+
const route = require(path.join(apiDir, file));
38+
const routeName = '/' + file.replace('.js', '');
39+
app.use(routeName, route);
40+
}
41+
});
42+
};`,
43+
'api/user.js': `const express = require('express');
44+
const router = express.Router();
45+
46+
router.get('/', (req, res) => {
47+
res.json({ message: 'User endpoint' });
48+
});
49+
50+
module.exports = router;`
51+
};
52+
53+
function createProject() {
54+
if (fs.existsSync(projectPath)) {
55+
console.error('Error: Project folder already exists.');
56+
process.exit(1);
57+
}
58+
59+
fs.mkdirSync(projectPath, { recursive: true });
60+
fs.mkdirSync(path.join(projectPath, 'api'));
61+
62+
for (const [file, content] of Object.entries(files)) {
63+
const filePath = path.join(projectPath, file);
64+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
65+
fs.writeFileSync(filePath, content);
66+
}
67+
68+
console.log('Example project created!');
69+
console.log(`Run:
70+
cd ${projectName}
71+
npm install
72+
node server.js`);
73+
}
74+
75+
createProject();

0 commit comments

Comments
 (0)