0.3.1 • Published 6 years ago

express-middleware-file-routes v0.3.1

Weekly downloads
5
License
ISC
Repository
github
Last release
6 years ago

express-middleware-file-routes

A lightning fast middleware for express to transform a folder structure into an API.

NPM Version NPM Downloads Github Issues License

How does it work?

By looking in a folder (default: /routes) - API routes are created based on the file structure.

A folder or a file would create the same structure like so:

/home.js -> /home and /home/index.js -> /home

(what type of files that are allowed to created controllers can be configured by the fileTypes option)

Get started

Installation

npm install express-middleware-file-routes

Basic Setup

const express = require('express');
const fileBasedApi = require('express-middleware-file-routes');

// load express
const app = express();

// this would point the root of your server to the api middleware
// accessible via /{your}/{path}
app.use(fileBasedAPI());

// it can also be mounted under a sub path like so:
// accessible via /api/{your}/{path}
app.use('/api', fileBasedAPI());

// boot your app on locahost:3000
app.listen(3000);

Defining your first controller

Inside your /routes folder - create a file called index.js

module.exports = {

    get: (req, res) => {
        res.json('You succeeded!');
    },

    post: (req, res) => {
        res.json('A post has been made, your highness');
    }

};

You should now be able to navigate to localhost:3000 in your browser and see the "You succeeded!" message.

Likewise, a post to the same route should give you the "A post has been made, your highness" message.

Advanced usage

Since the express router is very powerful, you could utilize that power for some fun magic.

For example - putting a file (or folder) name within parentheses /home/(whatHome).js would create a dynamic parameter just like express would with the /home/:whatHome syntax.

Suppose this being your (whatHome).js file:

module.exports = {

    get: (req, res) => {
        res.json(req.params);
    }

};

Accessing localhost:3000/home/my-awesome-home would go through the above controller and return this message:

{
    "whatHome": "my-awesome-home"
}

Options

Options can be passed into the constructor like so:

app.use(fileBasedAPI({
    optionName: 'value'
}));

beforeSetupRoute

(this hook will run before mounting each and every route found)

optionName: beforeSetupRoute

type: callable(route, controller)

defaultValue: (void)

fileTypes

(what type of files should be loaded as controllers)

optionName: fileTypes

type: array[string]

defaultValue: ['js']

rootFile

(name of the file that defines the root state / of a folder - defaults to index(.js))

optionName: rootFile

type: string

defaultValue: 'index'

routes

(path to the directory where routes are located)

optionName: routes

type: string

defaultValue: '/routes'

unsupportedMethodHandler

(fallback handler when no specific handler POST|GET|.. is found on a route)

optionName: routes

type: callable(req, res)

defaultValue: Returns status code 405 (method not supported)

License

Copyright (c) 2017 Anton Netterwall

Available under the MIT license. See the LICENSE file for more info.

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago