0.0.2-alpha.3 • Published 11 months ago

@kromi77/express-rh v0.0.2-alpha.3

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@kromi77/express-rh

Table of contents

☕️ About ERH

The package was created from a spontaneous idea that came up while my friends and I were working on a joint project (a social platform) for a university course. Since we used Express to build the backend for our project, I took on the task of creating a route handler, which became the foundation of the current state of the package you see here. I really liked Next.js and the way it handles routing, so I wanted this handler to closely replicate that behavior.

📀 Requirements

Express.JS Node.JS or Bun

🔧 Installation

For npm:

npm i @kromi77/express-rh

For bun:

bun add @kromi77/express-rh

🚀 Quick start

ESM

//index.ts
import ERH from "@kromi77/express-rh";
import express from "express";
import path from "path";
const routes = new ERH({
	app: express(),
	endpointsFolder: path.join(__dirname, "endpoints"),
	useParentPath: true,
	baseRoute: "/api/v1/",
});
// endpoints/get/users.ts
import { Response, Request } from "express";
export default {
	callback: (req: Request, res: Response) => {
		res.send("Hello from GET/users.js!");
	},
};

CJS

//index.js
const { ERH } = require("@kromi77/express-rh");
//OR
//const ERH = require('@kromi77/express-rh').default;
const express = require("express");
const path = require("path");
const routes = new ERH({
	app: express(),
	endpointsFolder: path.join(__dirname, "endpoints"),
	useParentPath: true,
	baseRoute: "/api/v1/",
});
// endpoints/get/users.js
module.exports = {
	callback: (req, res) => {
		res.send("Hello from GET/users.js!");
	},
};

📖 Documentation

ERH Options

OptionTypeRequiredDefaultDescription
appexpress.Application-Instance of express application
endpointsFolderstring-Path to folder with endpoints. Inside this folder remember to create folders that will match naming of methods like GET, POST, PUT etc.
hoststringlocalhostHost to listen on
portnumber3000 for public 8080 or 0 for devPort to listen on
baseRoutestring/Base api route that will be added before endpoint ex. /api/v1/ then endpoint will look like /api/v1/yourendpoint
devModebooleanfalseIf dev server should be created
devModeRoutestring/devYou route that will be added before dev endpoint ex. /dev/v2 then endpoint will look like /dev/v2/yourendpoint
useBuiltInLoggerbooleantrueWhether you want to use built in logger to log every endpoint call
useBuiltInRateLimiterbooleantrueWhether you want to use built in rate limiter that using express-rate-limit with values described in usage section
useBuiltInCorsbooleantrueWhether you want to use built in cors from cors package
useBuiltInJsonParserbooleantrueWhether you want to use built in json parser from express
useBuiltInUrlEncodedParserbooleantrueWhether you want to use built in url encoded parser from express
useParentPathbooleanfalseWhether to use directory structure as base path for endpoints
middlewareFolderstring-Folder with middlewares
staticFoldersstring[]-List of folders with static content
staticBasePathstring/staticBase api route for static files
startCallbackfunctionsimple logWhether you want to overwrite base server start callback

Interface IMiddlewareModule

PropertyTypeRequiredDefaultDescription
callbackfunction-Callback function with functionality of middleware
namestring-Name of middleware module
globalbooleantrueWhether middleware should be used among all endpoints
matcherstring-Whether middleware should be used matched paths
ignorebooleanfalseWhether middleware should be ignored from setting up

Interface IEndpointModule

PropertyTypeRequiredDefaultDescription
pathstring-Override the default path of the endpoint
middlewaresIMiddlewareModule [][]Define middleware to run on endpoint call
callbackFunction-Callback function with endpoint functionality

🔍 Good to know

  • If you use one of --public, -p, --host, -h flags then handler will try to host your app publicly

  • Adding . in name of your endpoint file will extend a path eg. products.list.js will result path: basepath/products/list

  • Using [id] as your folder name will result dynamic path it's same as using :id

🚨 Issues

If you see any improvements to make or found a bug feel free to open an issue with this template

Type: Issue | Improvement
Description of issue or improvement:
Express version:
NodeJS version:
ERH version:
Additional info: