Coder Social home page Coder Social logo

express-route-controller2's Introduction

express-route-controller2

This is a helper function to assign controller actions to routes in express based on express-route-controller.

It provides just a structure on top of a normal express app, but also doesn't get in the way at all, and you can continue to use express normally and define even more routes manually if you wish. It's focused to help in the creation of APIs.

Usage

In your express project install express-route-controller2:

npm install express-route-controller2

Now create a folder where you want all your controllers, eg. controllers, and add a file in there, named mycontroller.js. Then define it somehow, like so:

module.exports = {
    create: function(req, res) {
    },
    update: function(req, res) {
    },
    read: function(req, res) {
    },
    delete: function(req, res) {
    },
    validate: function(req, res, next) {
    }
};

In your main app.js file (or wherever you set up express routes normally) simply call the helper function (very sparse demo express app):

var express = require('express');
var app = express();
var erc2 = require('express-route-controller2');

// set up express route control:
erc2(app, {
  controllers: __dirname + '/controllers',
  routes: {
    "/grades": "mycontroller#read", // method: get
    "/notices/:id": { 
      "get": "mycontroller#read",
      "delete": "mycontroller#delete",
      "put": {
        "middleware": "mycontroller#validate", // this could be an array eg: ["mc#1", "mc#2"]
        "handler": "mycontroller#update"
      }
    }
  }
});

app.listen(3000);

You can make this even more easier, by defining your routes in a routes.json file, like so:

{
  "/grades": "mycontroller#read", 
  "/notices/:id": { 
    "get": "mycontroller#read",
    "delete": "mycontroller#delete",
    "put": {
      "middleware": "mycontroller#validate",
      "handler": "mycontroller#update"
    }
  }
}

And loading the routes is as simple as:

// ...

erc2(app, {
    controllers: __dirname + '/controllers',
    routes: require('routes.json')
});

// ...

Feedback

Pull requests, feature ideas and bug reports are welcome.

License

MIT.

express-route-controller2's People

Contributors

andresz1 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.