Code Monkey home page Code Monkey logo

express-load-router's Introduction

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);
option type default
excludeRules Array []
rewriteRules Map new Map()

Controller declaration

There are three kinds of Controller for this package:

  • Plain function
exports.api = (req, res) => {
  res.send('API');
};
  • Object
Property Type Required Default Note
method String No GET one of ['GET', 'POST', 'PUT', 'DELETE']
params Array No []
middlewares Array No [] Array of middlewares, see below
handler Function Yes --

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

express-load-router's People

Contributors

sfantasy avatar vigks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

express-load-router's Issues

Add a different way of router definition

Full version of the definition, an array of different methods:

exports.path_name = [{
  method: 'GET',
  parameters: [':id'],
  middlewares: [m1, m2],
  handler(req, res) {}
}, {
  method: 'POST',
  handler(req, res) {}
}];

Short hand:

  • Function
  • Single Object

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.