Giter Club home page Giter Club logo

expressdirectoryrouter's Introduction

ExpressDirectoryRouter

Version package DL TOTAL LATEST RELEASE ALL CONTRIBUTOR

ExpressDirectoryRouter is a lightweight and flexible middleware for Express.js that simplifies routing by automatically handling routes based on the structure of your project directories.

Table of Contents

Installation

You can install ExpressDirectoryRouter using npm, yarn, or pnpm for example:

npm install @kyri123/express-directory-router

yarn add @kyri123/express-directory-router

pnpm add @kyri123/express-directory-router

Usage

To use ExpressDirectoryRouter, simply require it and use it as middleware in your Express application:

Javascript

const express = require('express');
const { installAppExpress } = require('express-directory-router');

const app = express();

// install routings with default settings
installAppExpress(app).then(() => {
	app.listen(3000, () => {
		console.log('Server is running on http://localhost:3000');
	});
});

Typescript

import express from 'express';
import { installAppExpress } from 'express-directory-router';

const app = express();

// install routings with default settings
installAppExpress(app).then(() => {
	app.listen(3000, () => {
		console.log('Server is running on http://localhost:3000');
	});
});

Middleware

Middleware can be used in the following formats the name of the file must ne middleware.(js|ts) Following named exports are supported:

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH
  • GLOBAL -> GLOBAL is a special middleware that is executed for all routes and methods (also in subdirectories)
  • WILDCARD -> WILDCARD is a special middleware that is executed for all routes and methods (BUT NOT in subdirectories)
  • HEAD
  • OPTIONS
  • ALL
  • TRACE
import { type MiddleWareInit } from '@kyri123/express-directory-router';

export const GET: MiddleWareInit = async (payload) => {
	return [
		(req, res, next) => {
			return next();
		}
	];
};

export const GLOBAL: MiddleWareInit = async (payload) => {
	return [
		(req, res, next) => {
			return next();
		}
	];
};

Directory-Routes

Route the directory structure is as follows:

Directory Description Directory Express Route
XYZ name of the Route for exmaple xyz/123 /xyz/123
[paramname] will used for params xyz/[paramname] /xyz/:paramname
[paramname...] will used for params and route all subdirectorys here xyz/[paramname...] /xyz/:paramname*
(xyz) will ignored for routing so used for organisation xyz/(group)/123 /xyz/123
[...] Will route all subdirectory here xyz/[...] /xyz/*
XYZ name of the Route for exmaple xyz/123 /xyz/123

Route-Functions

Routes can be used in the following formats the name of the file must ne ('get'|'post'|'put'|'delete'|'all'|'head'|'connect'|'options'|'trace').(js|ts): named exports:

  • middleware -> (optional) - Middleware for the route (see Middleware)
  • default -> (required) - The route handler must be a RoutingFunction
```ts
import { ResponseStatus, jsend, type MiddleWareInit, type RoutingFunction } from '@kyri123/express-directory-router';

export const middleware: MiddleWareInit = async (payload) => {
	return [
		(req, res, next) => {
			return next();
		}
	];
};

const handler: RoutingFunction = async ({ response, request, next, payload }) => {
	response.status(200).json(
		jsend(ResponseStatus.Success, {
			data: { message: 'Hello World' }
		})
	);
};

export default handler;

Documentation

You can find the detailed documentation for ExpressDirectoryRouter in the docs directory.

Contributing

We welcome contributions! If you find a bug or have an idea for a new feature, please open an issue or submit a pull request.

Please make sure to follow our code of conduct.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

Thanks to the contributors of Express.js for providing a powerful web application framework.


expressdirectoryrouter's People

Contributors

kyri123 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.