0.0.2 • Published 6 years ago

excir v0.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

excir

A simple router builder for Express and Koa.

How to install?

npm i excir

How it works

TermWhat it means?Example
routeHTTP Endpoint/pets
methodHTTP Request MethodGET
controllerExpress middleware function namegetPet

The value for Controller is the name of the file that contains the implementation. Excir will refer to the function by its filename. Currently, Excir does not take nested directory (namespace) into consideration. Thus, while having nested directory is possible, the function name has to be unique.

Example: getPet.js

// getPet.js
module.exports = (req, res) => {
    res.send("Pet");
}

Excir goes through all defined routes and methods in the specification list and try to map them to the controller that was defined.

You do not need to require express in every Controller file.

Functions

Excir()

The heart of this library. Returns a router object/function depending on the backend selected.

walkDirectory()

A synchronous function to walk through the directory and retrieve the js files. Exposed for testing purposes.

Options

The Excir() function takes in a config object as its second argument.

PropertyWhat it means?TypeDefault valueValid values
backendWhich backend to usestringexpressexpress, koa

API spec

{
    controllerPath: string
    path: {
        "route": {
            "method": {
                "controller": string
            }
        }
    }
}

How to use

const express = require("express");
const Excir = require("excir").Excir;

var spec = {
    // The directory that contains all route implementations
    controllerPath: "controller"
    path: {
        "/": {
            // HTTP Request Method -> function
            /*
                getRoot.js
                ----------
                module.exports = (req, res) => {};
            */
            "get": {
                "controller": "getRoot"
            },
            "post": {
                "controller": "submitForm"
            }
        },
        "/pets": {
            "get": {
                "controller": "getPets"
            }
        },
        "/pets/{id}": {
            "get": {
                "controller": "getPetById"
            }
        }
    }
}

var app = express();
var router = Excir(spec);

app.use("/", router);
app.listen(8080);

Changelog

0.0.2

Added Koa routing support

0.0.1

Documentation

Future Plans

  • Expose the Excir function only in index.js.
0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago