# @kyri123/express-app-router

> [![Version package](https://badgen.net/npm/v/@kyri123/express-app-router)](https://npmjs.com/@kyri123/express-app-router) [![DL TOTAL](https://badgen.net/npm/dt/@kyri123/express-app-router)](https://npmjs.com/package/@kyri123/express-app-router) [![LATEST

Latest version **1.1.0** (published 2023-10-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install @kyri123/express-app-router
pnpm add @kyri123/express-app-router
yarn add @kyri123/express-app-router
bun add @kyri123/express-app-router
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2023-10-07 |
| First published | 2023-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 96.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Oliver Kaufmann |
| Maintainers | kyri123 |

## Links

- npm: https://www.npmjs.com/package/@kyri123/express-app-router
- npm.io page: https://npm.io/package/@kyri123/express-app-router

## Dependencies (4)

- [zod](https://npm.io/package/zod.md) ^3.22.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [express](https://npm.io/package/express.md) ^4.18.2
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.6

## Recent versions

- 1.1.0 (latest) — 2023-10-07
- 1.0.9 — 2023-10-03
- 1.0.8 — 2023-10-03
- 1.0.3 — 2023-10-03
- 1.0.0 — 2023-10-02

## README

# ExpressDirectoryRouter

[![Version package](https://badgen.net/npm/v/@kyri123/express-app-router)](https://npmjs.com/@kyri123/express-app-router) [![DL TOTAL](https://badgen.net/npm/dt/@kyri123/express-app-router)](https://npmjs.com/package/@kyri123/express-app-router) [![LATEST RELEASE](https://badgen.net/github/release/Kyri123/ExpressDirectoryRouter)](https://npmjs.com/package/embedgenerator) [![ALL CONTRIBUTOR](https://badgen.net/github/contributors/Kyri123/ExpressDirectoryRouter)](https://npmjs.com/package/embedgenerator)

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](#installation)
-   [Usage](#usage)
    -   [JavaScript](#javascript)
    -   [TypeScript](#typescript)
    -   [Middleware](#middleware)
    -   [Directory-Routes](#directory-routes)
    -   [Route-Functions](#route-functions)
-   [Documentation](https://github.com/Kyri123/ExpressDirectoryRouter/tree/docs/doc)
-   [Contributing](#contributing)
-   [License](#license)

## Installation

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

```bash
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

```js
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

```js
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`

```ts
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](#middleware))
-   `default` -> (required) - The route handler must be a RoutingFunction

````ts


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

```

```

---
_Source: https://npm.io/package/@kyri123/express-app-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
