https://github.com/aquaticcalf/file-system-api-router
a file based api wrapper for express
https://github.com/aquaticcalf/file-system-api-router
api based express file
Last synced: 11 months ago
JSON representation
a file based api wrapper for express
- Host: GitHub
- URL: https://github.com/aquaticcalf/file-system-api-router
- Owner: aquaticcalf
- License: mit
- Created: 2025-03-16T08:01:20.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2025-03-17T08:28:16.000Z (11 months ago)
- Last Synced: 2025-03-17T08:35:12.314Z (11 months ago)
- Topics: api, based, express, file
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/file-system-api-router
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
## file system api router
### installation :
```bash
npm install file-system-api-router
```
### usage in your app :
```js
// index.js
import express from 'express'
import { registerRoutes } from 'file-system-api-router'
const app = express()
app.use(express.json())
// automatically register API routes from the "api" folder
await registerRoutes(app, 'api');
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
console.log(`server is running on port ${PORT}`)
})
```
### file structure convention :
```
api/
users/
get.js -> GET /users
[userId]/
get.js -> GET /users/:userId
post.js -> POST /users/:userId
posts/
new/
post.js -> POST /posts/new
[postId]/
get.js -> GET /posts/:postId
put.js -> PUT /posts/:postId
delete.js -> DELETE /posts/:postId
hello/
index/
get.js -> GET /hello
[username]/
get.js -> GET /hello/:username
```
### features :
1. automatically maps your file system structure to API endpoints
2. supports dynamic parameters with `[param]` folder names
3. supports method-specific endpoint files (e.g. `get.js`, `post.js`, etc.)
4. supports nested routes and index routes for cleaner organization
### for your API endpoint files :
```js
// api/users/[userId]/get.js
export default function getUser(req, res) {
const { userId } = req.params
res.json({ message: `fetching user with id ${userId}` })
}
// api/posts/[postId]/put.js
export default function updatePost(req, res) {
const { postId } = req.params
res.json({ message: `updating post with id ${postId}`, data: req.body })
}
```