2.1.4 • Published 8 years ago

express-load-router v2.1.4

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

express-load-router

Load routers from specific folders for Express.js

NPM version npm download David deps

中文文档

Install

npm i express-load-router -S

Usage

const path = require('path');
const express = require('express');
const loadRouter = require('express-load-router');

const app = express();

// Use `path.join(__dirname, 'path/to/folder')` here
loadRouter(app, path.join(__dirname, 'controllers'));

Options

loadRouter(app, path.join(__dirname, 'controllers'), options);
optiontypedefault
excludeRulesArray[]
rewriteRulesMapnew Map()

Controller declaration

There are three kinds of Controller for this package:

  • Plain function
exports.api = (req, res) => {
  res.send('API');
};
  • Object
PropertyTypeRequiredDefaultNote
methodStringNoGETone of 'GET', 'POST', 'PUT', 'DELETE'
paramsArrayNo[]
middlewaresArrayNo[]Array of middlewares, see below
handlerFunctionYes--

e.g.

exports.api = {
  method: 'GET',
  params: [':id'],
  handler(req, res) {
    res.send('API');
  }
};

Middlewares support

This package also support middlewares in controller.

e.g.

exports.api = {
  method: 'GET',
  params: [':id'],
  middlewares: [
    function (req, res, next) {
      console.log('Middleware 1');
      next();
    },
    function (req, res, next) {
      console.log('Middleware 2');
      next();
    },
  ],
  handler(req, res) {
    return res.send(`product detail ${req.params.id}`);
  },
};

Example

See example.

License

The MIT License

2.1.4

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.3

10 years ago

2.0.2

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago