2.0.3 • Published 4 months ago

express-dynamic-router-creator v2.0.3

Weekly downloads
4
License
ISC
Repository
github
Last release
4 months ago

Express Dynamic Router Creator

With this module you will be able to write dynamic created routing with express.

Features

  • Change the folders required for the dynamic router.
    • Routers Folder
    • Middlewares Folder
    • Controllers Folder
  • Define the main route file in the defined route folder.

Configuration

'use strict';

const express = require('express');
const app = express();
const path = require('path');

const DynamicRouter = require('express-dynamic-router-creator');

new DynamicRouter({
  app,
  folders: {
    routers: path.join(__dirname, 'routers'),
    controllers: path.join(__dirname, 'controllers'),
    middlewares: path.join(__dirname, 'middlewares'),
  },
  routerFiles: ['api/main', 'client'],
}).run();

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

Info: The files or files you define in mainFile work independently of each other. For example, if you have a router for the front of a route file, then the other file contains the necessary routing for the background. When defining a file, you need to enter the file name into. If you want to define more than one file, you must define your files in an array.

The output of the open log feature.

Log Image

Code Examples

  1. Basic Example
  2. Controller Example
  3. Middleware Example
  4. Multiple Example
  5. Versioning Example
  6. One File Example

Config Params

new DynamicRouter({
  app,
  port: process.env.PORT || 3000,
  folders: {
    routers: path.join(__dirname, 'routers'),
    controllers: path.join(__dirname, 'controllers'),
    middlewares: path.join(__dirname, 'middlewares'),
  },
  routerFiles: ['api/main', 'client'],
  info: true,
});

Or

new DynamicRouter({
  app,
  port: process.env.PORT || 3000,
  routers: [
    {
      url: 'api',
      method: 'get',
      middlewares: [
        (req, res, next) => {
          res.setHeader('x-app', 'express-dynamic-router-creator');
          next();
        }
      ],
      routes: [
        {
          url: 'users',
          routes: [
            {
              action: (req, res) => {
                res.send('user list');
              },
            },
          ],
        },
      ],
    },
  ],
});
ParamDescription
appSend the express app parameter. Required
routersRouters are defined here. Must be array. Optional
portSend the express port parameter. if the port is set you don't need to do listen. Optional
foldersThe folder definitions required for the project are given here. routers, controllers and middlewares parameters can be sent. Must be object. Required but, optional if routers is defined
routerFilesRoute files are defined here. Must be array. Required but, optional if routers is defined
infoThis parameter is sent to the console to print information. Must be boolean. Default value true. Optional

Route File Params

[
  {
    url: 'api',
    method: 'get',
    middlewares: [
      (req, res, next) => {
        res.setHeader('npm-module', 'express dynamic router creator');
        next();
      },
    ],
    routes: [
      {
        url: 'users',
        middlewares: ['auth.middleware'],
        controller: 'users.controller',
        routes: [
          {
            action: 'getAll',
          },
          {
            method: 'post',
            action: 'add',
          },
          {
            url: ':id',
            routes: [
              {
                method: 'put',
                action: 'update',
              },
              {
                method: 'delete',
                action: 'delete',
              },
              {
                action: 'get',
              },
            ],
          },
        ],
      },
      {
        key: 'not_found_handler',
        method: 'use',
        action: (req, res, next) => {
          next({
            status: 404,
            message: 'Not Found',
          });
        },
      },
      {
        key: 'error_handler',
        method: 'use',
        action: (error, req, res, next) => {
          res
            .status(error.status || 500)
            .send(error.message || 'Internal Server Error');
        },
      },
    ],
  },
];
ParamDescriptionDefault Values
methodThe route is defined by which request to work. If the method is not sent and if it is in a sub-route, it takes the method of the next route un. Must be string. It can be optional according to the upper route.
urlThe route url is defined. if this parameter is not sent '/' is defined. If it is located in a sub-route group, it is combined with the top route urls. Must be string. It can be optional according to the upper route./
controllerThe name of the file that the route is running. It searches the action function in this file. Must be string. It can be optional according to the upper route.
actionRepresents the function in which route operations are performed. Must be string or function. Required
middlewaresUsed to define the middleware of the route. Must be array in string or function. It can be optional according to the upper route.
statusTo make the route u active or passive. By default, the route is active. Must be boolean. Optionaltrue
keyUsed to distinguish routes. If no value is entered, it will be created automatically according to the url. Optional
routesIt is used to group routes. The route properties of this parameter are copied to the subroutines. Must be array and it should consist of Route File Params. Optional
2.0.3

4 months ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago