1.0.7 • Published 2 years ago

express-easy-routes v1.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Express Easy Routes

npm GitHub

Provides an easy to setup routes for an express api based on a folder hierarchy.

/routes
    - index.ts                                      == /
    /test
        - index.ts                                  == /test
        - $id.ts                                    == /test/:id
        - $id.edit.$name.ts                         == /test/:id/edit/:name

Quick blog post that sheds some light into this library.

Learn the easy way to handle routes with Express.js

Basic example

Each route is created as a class example of this is below

/** imagine the following file (index.ts) sits at the root of /routes like such /routes/index.ts.
 *  When this file gets initialized by initRoutes it will get converted to the following.
 *  /routes/index.ts    ==    /
 *  meaning this is the root path to our API
 **/

import { BaseRoute, IRouteDef } from 'express-easy-routes';

/**
 * The BaseRoute abstract class provides public member variables that allow you to define the basic http methods that you will normally use.
 **/
export default class Root extends BaseRoute {
  //This flag is used to mark if this route is set to active, meaning it will get set up when we start our application.
  active: boolean = true;

  //The following public member variable in BaseRoute can be overridden to provide your middleware for the get HTTP method for this route and has an additional active flag for granular control over HTTP methods for each route. 
  get: IRouteDef = {
    active: true,
    //Here chain is an array of middleware
    chain: [
      async (req, res, next) => {
        return res.send({ message: 'hello from root', version: 0 });
      },
    ],
  };
}
const express = require("express");
const { initRoutes } = require('express-easy-routes');
or;
import express from 'express';
import { initRoutes } from 'express-easy-routes';

const app = express();


//In this case my routes are in the 'routes' folder
app.use(
  '/',
  initRoutes({
    path: __dirname + '/routes',
  }),
);

//rest of initialization
1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago