npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-dynamic-router-creator

v2.0.3

Published

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

Downloads

40

Readme

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');
              },
            },
          ],
        },
      ],
    },
  ],
});

| Param | Description | |-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | app | Send the express app parameter. Required | | routers | Routers are defined here. Must be array. Optional | | port | Send the express port parameter. if the port is set you don't need to do listen. Optional | | folders | The 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 | | routerFiles | Route files are defined here. Must be array. Required but, optional if routers is defined | | info | This 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');
        },
      },
    ],
  },
];

| Param | Description | Default Values | | --------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | | method | The 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. | | | url | The 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. | / | | controller | The 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. | | | action | Represents the function in which route operations are performed. Must be string or function. Required | | | middlewares | Used to define the middleware of the route. Must be array in string or function. It can be optional according to the upper route. | | | status | To make the route u active or passive. By default, the route is active. Must be boolean. Optional | true | | key | Used to distinguish routes. If no value is entered, it will be created automatically according to the url. Optional | | | routes | It 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 | |