file-system-api-router
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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 :

  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 :

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

Readme

Keywords

Package Sidebar

Install

npm i file-system-api-router

Weekly Downloads

11

Version

1.0.1

License

MIT

Unpacked Size

6.5 kB

Total Files

5

Last publish

Collaborators

  • aquaticcalf