Skip to content

Latest commit

 

History

History
236 lines (179 loc) · 3.95 KB

README.MD

File metadata and controls

236 lines (179 loc) · 3.95 KB

Exha

Fast, compact, open source route and middleware handler for expess

Fast Setup

npx exhapack your_app_name
//index.js
const { Exha } = require("exha");
const exha = new Exha();

exha.init();

Installation

This is a Node.js module avaliable on the npm registry

Installation is done using the:

$ npm install --save-dev exha

Features

  • Easily file routings
  • Easily file middlewares for routings
  • Create application quickly
  • Built-in image uploader

Table of Contents

Setup

Create a /root directory, create index.js file, create exha.config.js file, create /routes directory and create /index file in routes directory

Looks like;

project_root/
│
├── /routes
│   └── index.js
│
├── exha.config.js
│
└── index.js

project_root/index.js

const { Exha } = require("exha");
let exha = new Exha();

exha.init();

project_root/exha.config.js

exports.default = {
    port: 3000,
    routeDir: "/routes",
};

project_root/routes/index.js

module.exports = {
    event: (req, res) => {
        res.send("Hello world");
    },
};

Middleware uses

Add middlewareDir properties to exha config

exports.default = {
    port: 3000,
    routeDir: "/routes",
    middlewareDir: "/middlewares",
};

and create a new middleware in /middlewares directory

project_root/middlewares/check.js

module.exports = {
    middleware: (req, res, next) => {
        console.log("A new request!");
        next();
    },
};

and go back to routes/index

const {mwLoader} = require("exha")
let checkMw = mwLoader("check.js")

module.exports = {
    middlewares = [checkMw],
    event: (req,res) => {
        res.send("Hello world")
    }
}

Plugin uses

Plugin usage is very similar to middleware. Add pluginDir properties to exha config

exports.default = {
    port: 3000,
    routeDir: "/routes",
    middlewareDir: "/middlewares",
    pluginDir: "/plugins",
};

and create a new plugin in /plugins directory

project_root/plugins/whoReq.js

module.exports = {
    plugin: (name) => {
        console.log(name, " requested");
    },
};

and go back to routes/index

const {mwLoader,plLoader} = require("exha")
let checkMw = mwLoader("check.js")
let whoReq = plLoader("whoReq.js")

module.exports = {
    middlewares = [checkMw],
    event: (req,res) => {
        whoReq("John Doe")
        res.send("Hello world")
    }
}

Form parsing

Allows parsing of incoming requests

Add parseForm properties to exha config

exports.default = {
    port: 3000,
    routeDir: "/routes",
    middlewareDir: "/middlewares",
    pluginDir: "/plugins",
    parseForm: true,
};

Static files

Serving static files

Add staticDir properties to exha config

exports.default = {
    port: 3000,
    routeDir: "/routes",
    middlewareDir: "/middlewares",
    pluginDir: "/plugins",
    parseForm: true,
    staticDir: "public",
};

Image uploader

Built-in image uploader

Add imageUploader properties to exha config

exports.default = {
    port: 3000,
    routeDir: "/routes",
    middlewareDir: "/middlewares",
    pluginDir: "/plugins",
    parseForm: true,
    staticDir: "public",
    imageUploader: true,
};

Use can it in middleware

const {mwLoader,plLoader,uploader} = require("exha")
let checkMw = mwLoader("check.js")
let whoReq = plLoader("whoReq.js")

module.exports = {
    middlewares = [checkMw,uploader.single("image")],
    event: (req,res) => {
        console.log(req.files)
        whoReq("John Doe")
        res.send("Hello world")
    }
}