# dynamic-express-router

> A package that allows setting up routes dynamically

Latest version **0.5.0** (published 2019-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install dynamic-express-router
pnpm add dynamic-express-router
yarn add dynamic-express-router
bun add dynamic-express-router
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2019-12-11 |
| First published | 2019-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 48.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Muhammad Qasim |
| Maintainers | qasim9872 |
| Keywords | express, dynamic, routing |

## Links

- npm: https://www.npmjs.com/package/dynamic-express-router
- Repository: https://github.com/qasim9872/Dynamic-Express-Router
- Homepage: https://github.com/qasim9872/Dynamic-Express-Router#readme
- Issues: https://github.com/qasim9872/Dynamic-Express-Router/issues
- npm.io page: https://npm.io/package/dynamic-express-router

## Dependencies (7)

- [chalk](https://npm.io/package/chalk.md) ^2.4.2
- [debug](https://npm.io/package/debug.md) ^4.1.1
- [express](https://npm.io/package/express.md) ^4.17.1
- [is-what](https://npm.io/package/is-what.md) ^3.2.3
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.0.1
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.0
- [express-promise-router](https://npm.io/package/express-promise-router.md) ^3.0.3

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 0.5.0 (latest) — 2019-12-11
- 0.1.1-0 (next) — 2019-06-22
- 0.4.1 — 2019-07-07
- 0.4.0 — 2019-07-07
- 0.3.0 — 2019-07-07
- 0.2.0 — 2019-06-22

## README

# Dynamic Express Router

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

A simple to use Express based library that automatically generates routes from a specified directory (default: /routes). Furthermore, allows for dynamic configuration of middlewares by specifying a middlewares folder (default: /middlewares) and using them in the routes using the middleware name.

## Getting Started

With NPM install the module with: `npm install dynamic-express-router --save`  
With YARN install the module with: `yarn add dynamic-express-router`

## Setup

To use the package, simply configure and use the package with express app.

```typescript
import express from "express"
import dynamicRouter from "dynamic-express-router"

export default async function setup() {
    const app = express()

    // Additional middlewares here

    const router = await dynamicRouter({
        baseDir: __dirname, // required
        middlewares: "./middlewares", // default - relative to baseDir
        routes: "./routes", // default - relative to baseDir
        fileExtensions: [".ts", ".js"], // default
        filters: [/\.d\.ts$/] // default - ignore type files
    })
    app.use(router)

    return app
}
```

## Example Routes File

export a function (handler) or a string array (middleware names) as you like, simply include the request type in the name and the package will pick it up.

```typescript
import { Request, Response } from "express"

// the array name contains the request type: get
export const getMiddlewares = ["name-of-middleware-file"]

// the function name contains the request type: get
export async function getHandler(req: Request, res: Response) {
    return res.sendStatus(200)
}

// the function name contains the request type: post
export async function handlePost(req: Request, res: Response) {
    return res.sendStatus(200)
}
```

## Example Middleware File

```typescript
// file: middlewares/middleware-a.ts
// can be used in routes using the name "middleware-a"
import { Request, Response, NextFunction } from "express"

export async function checkParamMiddleware(req: Request, res: Response, next: NextFunction) {
    if (req.body && req.body.error) {
        res.sendStatus(500)
    } else {
        next()
    }
}
```

## Routes Generated Dynamically

The below folder structure

```code
/middlewares
    middleware-a.ts
    middleware-b.ts
/routes
    health-check.ts
    /user
        index.ts
        _id.ts
```

Will Generate the following routes

```routes

/health-check
/user
/user/:id

```

## Debugging

set environment variable to below when running your application.

```cli
DEBUG=dynamic-express-router:\*
```

## Changelog

See changelog.md

## Issues

If you discover a bug, please raise an issue on Github. https://github.com/qasim9872/Dynamic-Express-Router/issues

## Contribution

Pull requests are very welcome. Please:

-   ensure all tests pass before submitting PR
-   add an entry to CHANGELOG.md
-   add tests for new features
-   document new functionality/API additions in README.md

## License

Copyright (c) 2019 Muhammad Qasim. Licensed under the MIT license.

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