1.1.0 • Published 7 months ago

@kyri123/express-app-router v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

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:

DirectoryDescriptionDirectoryExpress Route
XYZname of the Route for exmaplexyz/123/xyz/123
[paramname]will used for paramsxyz/[paramname]/xyz/:paramname
[paramname...]will used for params and route all subdirectorys herexyz/[paramname...]/xyz/:paramname*
(xyz)will ignored for routing so used for organisationxyz/(group)/123/xyz/123
[...]Will route all subdirectory herexyz/[...]/xyz/*
XYZname of the Route for exmaplexyz/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.

1.1.0

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.0

7 months ago