Coder Social home page Coder Social logo

express-enrouten's Introduction

express-enrouten

Route configuration middleware for expressjs.

API

app.use(enrouten(options))

var express = require('express'),
    enrouten = require('express-enrouten');

var app = express();
app.use(enrouten({ ... });

#### enrouten(app).withRoutes(options)

var express = require('express'),
    enrouten = require('express-enrouten');

// Legacy API - do not use
//var app = express();
//enrouten(app).withRoutes({ ... });

Configuration

express-enrouten supports routes via configuration and convention.

app.use(enrouten({
        method: 'get',
        path: '/foo',
        handler: function (req, res) {
            // ...
        }
    }]
});
  • directory (optional) - String or array of path segments. Specify a directory to have enrouten scan all files recursively to find files that match the controller-spec API.
app.use(enrouten({
    directory: 'controllers'
});
  • routes (optional) An array of route definition objects. Each definition must have a path and handler property and can have an optional method property (method defaults to 'GET').
app.use(enrouten({
    routes: [
        { path: '/',    method: 'GET', handler: require('./controllers/index') },
        { path: '/foo', method: 'GET', handler: require('./controllers/foo') }
    ]
});
  • index (optional, overrides directory and disables scanning) - String path or array of path segments indicating the file to load which acts as the route 'index' of the application.
// index.js
module.exports = function (app) {

    app.get('/', index);
    app.get('/account', passport.protect, account);

    // etc...
};

Controller Files

A 'controller' is defined as any javascript file (extension of .js) which exports a function that accepts a single argument. NOTE: Any file in the directory tree that matches the API will be invoked/initialized with the express application object.

// Good :)
// controllers/controller.js
module.exports = function (app) {
    app.get('/', function (req, res) {
        // ...
    });
};

// Bad :(
// Function does not get returned when `require`-ed, use `module.exports`
exports = function (app) {
    // ...
};

// Bad :(
// controllers/other-file-in-same-controller-directory.js
modules.exports = function (config) {
    // `config` will be the express application
    // ...
};

// Acceptable :)
// controllers/config.json - A non-js file (ignored)
// controllers/README.txt - A non-js file (ignored)
// controllers/util.js - A js file that has a different API than the spec (ignored)
module.exports = {
    importantHelper: function () {

    }
};

express-enrouten's People

Contributors

totherik avatar jeffharrell avatar tlivings avatar lensam69 avatar

Watchers

Navid Nikpour 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.