GithubHelp home page GithubHelp logo

sirgolem / express-filesystem-router Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 60 KB

An easy-to-use filesystem router for ExpressJS.

TypeScript 100.00%
express expressjs filesystem nodejs typescript

express-filesystem-router's Introduction

express-filesystem-router

express-filesystem-router is a simple way to use the filesystem for ExpressJS route handling.

Features

  • TypeScript
  • Supports all Express route methods (with hyphens removed)
  • Supports middleware
  • Supports dynamic endpoints (Express parameters)
  • Ignore .d.ts and _-prefixed files (optional)

Creation

FSRouter(directoryPath: string, options?: RouterOptions)

  • directoryPath: The path to the folder containing the route files.

  • options (optional): The options for the router:

    • ignoreFilesPrefixedWithUnderscore: boolean (default true): Whether to ignore files with names that start with _ (e.g. _route.ts)

Method Matching

The method is inferred from the exported function name (e.g. get, post etc.).

function get(req: Request, res: Response) { ... }
function post(req: Request, res: Response) { ... }

// 'delete' is a reserved keyword, so 'del' can be used instead
function del(req: Request, res: Response) { ... }

// 'all' can be used for handling all methods
function all(req: Request, res: Response) { ... }

Middleware

If an exported Function[] called middleware is present it will be used as middleware for that route.

const middleware: Function[] = [ ... ];

export { middleware, ... }

Example

index.ts:

import * as express from 'express';
import { FSRouter } from 'express-filesystem-router';

const app = express();
const port = 3000;

app.use('/', FSRouter(__dirname + '/routes'));

app.listen(port, () => console.log(`App listening on port ${port}`));

routes/index.ts

import { Request, Response, NextFunction } from 'express';

const middleware: Function[] = [
    (req: Request, res: Response, next: NextFunction) => {
        console.log('Middleware!');
        next();
    },
];

function get(req: Request, res: Response) {
    res.status(200).send('Hello world!');
    return;
}

export { middleware, get };

express-filesystem-router's People

Contributors

sirgolem avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

 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.