GithubHelp home page GithubHelp logo

express-file-based's Introduction

express-file-based Middleware

File routing system-based framework like api serverless for Node Express with 0 dependencies.

Installation

npm install express-file-based

How to use

  • app.(js|ts) (main)
const express = require("express")
const { fileBased } = require("express-file-based")

const app = express()
app.use('/', fileBased()) // uses /routes directory by default
app.listen(2000)
  • routes/index.js
module.exports = async (req, res) => {
  if (req.method !== "GET") return res.status(404)

  return res.status(200)
}

Directory Structure

Files inside your project's /routes directory will get matched an url path automatically.

├── app.js
├── routes
    ├── index.js
    ├── posts
        ├── index.js
        └── :id.js or [id].js // dynamic req.params.id
    └── users.js
└── package.json
  • /routes → /
  • /routes/posts → /posts
  • /routes/posts/:id → /posts/:id
  • /routes/users → /users

API

app.use('/api', fileBased({
    directory: 'routes',
    methodExports: ["ws", ...]
  }))
  .use('/anotherrouter', fileBased({
    directory: 'anoherfile',
  }));

Options

  • directory: The path to the routes directory (default /routes)
  • methodExports: Additional method exports (e.g. ws for express-ws)
  • verbose: show routes on console (default process.env.NODE_ENV !== 'production' )
  • options: RouterOptions { caseSensitive/mergeParams/strict }

Examples

HTTP Method Matching

If you export functions named e.g. get, post, put, delete/del etc. those will get matched their corresponding http method automatically.

module.exports.priority = 1; // kind of like a z-index for your routes.

/**
 * @param {Express.Request} req
 * @param {Express.Response} res
 */module.exports.get = async (req, res) => { ... }

/**
 * @param {Express.Request} req
 * @param {Express.Response} res
 */module.exports.post = async (req, res) => { ... }


/**
 * @param {Express.Request} req
 * @param {Express.Response} res
 */module.exports.put = async (req, res) => { ... }

/**
 * @param {Express.Request} req
 * @param {Express.Response} res
 * it's not allowed to name variables 'delete': try 'del' instead
 */module.exports.del = async (req, res) => { ... }

/**
 * @param {Express.Request} req
 * @param {Express.Response} res
 * use default for _all
 */module.exports.default = async (req, res) => { ... }

Note: Named method exports gain priority over wildcard exports (= default exports).

Middlewares

You can add isolated, route specific middlewares by exporting an array of Express request handlers from your route file.

const { withAuth } = require("../middlewares")

module.exports.get = [
  withAuth,
  async (req, res) => { ... }
]

express-file-based's People

Contributors

mathec-x avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.