๐Ÿ“ฆ aquaticcalf / file-system-api-router

a file based api wrapper for express

โ˜… 1 stars โ‘‚ 0 forks ๐Ÿ‘ 1 watching โš–๏ธ MIT License
apibasedexpressfile
๐Ÿ“ฅ Clone https://github.com/aquaticcalf/file-system-api-router.git
HTTPS git clone https://github.com/aquaticcalf/file-system-api-router.git
SSH git clone git@github.com:aquaticcalf/file-system-api-router.git
CLI gh repo clone aquaticcalf/file-system-api-router
sam sam v1.0.1 79fdd28 10 months ago ๐Ÿ“ History
๐Ÿ“‚ master View all commits โ†’
๐Ÿ“„ .gitignore
๐Ÿ“„ index.d.ts
๐Ÿ“„ index.js
๐Ÿ“„ LICENSE
๐Ÿ“„ package-lock.json
๐Ÿ“„ package.json
๐Ÿ“„ readme.md
๐Ÿ“„ README.md

file system api router

installation :

npm install file-system-api-router

usage in your app :

// 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 :

  • automatically maps your file system structure to API endpoints
  • supports dynamic parameters with [param] folder names
  • supports method-specific endpoint files (e.g. get.js, post.js, etc.)
  • supports nested routes and index routes for cleaner organization

for your API endpoint files :

// 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 })
}