Skip to content

ricardjp/express-controller-routing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express Controller Routing

A simple way to create and test Express routes

Install

$ npm install express-controller-routing

Usage

Create a new Express controller file (./myRoute.js)

const myFirstMiddleware = function myFirstMiddleware(req, res, next) {
  next();
};

const mySecondMiddleware = function mySecondMiddleware(req, res, next) {
  next();
};

module.exports = {
  
  '/': {
    post: function createSomething(req, res, next) {
      // do something
    },
  },
  
  '/my/path/to/:something': {
    get: function getSomething(req, res, next) {
      // do something
    },
    
    // you can also use an array if you need to use middlewares
    post: [myFirstMiddleware, mySecondMiddleware, function createSomethingElse(req, res, next) {
      
    }],
  },
  
};

Register this newly created controller in app.js

const controller = require('express-controller-routing');
const express = require('express');
const app = express();

// expose all routes defined in ./myRoute.js
app.use(controller(require('./myRoute')));

// or if you need all routes in to be prefixed
app.use('/myPrefix', controller(require('./myRoute')));

About

A simple way to create and test Express routes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published