express-routes-loader

0.0.3 • Public • Published

express-routes-loader

A simple Express App utility to setup routes for the server.

Install

sudo npm install --save express-routes-loader

Usage

var path = require('path');
var app = require('express')();
var routesLoader = require('express-routes-loader');
/*...preprocess...*/
routesLoader(app, path.join(__dirname, 'routes');
 
/*...postprocess...*/
 
//At the end, start the server
if(require.main === module){
    app.listen(PORT);
} else {
//In case you want to use app in your tests.
  module.exports = app;
}
 

Routes directory

routes_dir

  • The second parameter passed into the loader function.

Routes for the app server will be registered automatically according to the directory's structure -- a route path is made of dirnames along the path from the root routes dir to the file that exports a router.

In order to automate the registration, you should use a index.js file to aggregate router-path mapping in each directories, and this can be easily done by utilizing npm module rqdir.

example for index file,

//the most simple way
module.exports = require('rqdir')(__dirname);
 
//or you need customize a little
module.exports = function(app){
  var router = require('express').Router();
  var appCfg = app.get('appConfig');
  router.get('/', someMiddleware(appCfg.someMiddleware));
  return require('rqdir')(__dirname);
};

Changelog

Latest:

0.0.1

  • Initial Commit

Readme

Keywords

none

Package Sidebar

Install

npm i express-routes-loader

Weekly Downloads

1

Version

0.0.3

License

MIT

Last publish

Collaborators

  • ybybzj