0.3.1 • Published 9 years ago

route-dir v0.3.1

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Recursively map directory structure to URIs

route-dir provides functionality to recursively map a directory structure to HTTP server URI supporting basic DSL syntax (in form of :id) in path names. The module was designed and tested with express, but does not have it as a dependency and will work in the same manner with other frameworks that use app.get, app.post etc. to register dispatch patterns. The module does not require that all routing patterns are registered in this manner and can be used in addition to any routing defined by other means.

Installation

$ npm install route-dir

Tests:

$ npm test

Usage

The code to handle requests needs to be arranged in a directory structure (e.g. server/routes) following these rules:

  • server/routes/index.js will map to /
  • server/routes/path/user/index.js will map to /path/user/
  • server/routes/path/user.js will map to /path/user (different from above)
  • server/routes/_colon_lang/user/_colon_id will map to /:lang/user/:id
  • server/routes/_star_ will map to /* as the last entry in the rule set

Request handlers are defined as functions get, post etc. exported by a module under server/routes:

exports.get = function(conn, config) {
    return function(req, res) {
        res.render("user/signup");
    };
};

exports.post = function(conn, config) {
    // 'conn' and 'server' can be used here e.g. to load a data model 
    // given a specific connection and using specific configuration
    return function(req, res, next) {
        var User = require("usermodel").getModel(conn);
        User.registerNewUser(req.body, function(err, user) {
            if (err) {
                next(err);
            } else {
                res.send(200, { userId: user.id });
                res.end();
            }
        });
    };
};

Here conn and config are optional arguments intended to pass a specific DB-connection (e.g. mongoose connection for connection-specific models as in the above example) and server configuration that may be needed in handling the request. Each exported function is expected to return a standard handler taking req, res or req, res, next arguments. These will be directly registered with the server:

...
var routing = require("route-dir");
var server = express();
...
server.use(server.router);
// register routes without passing connection or config to the handlers
routing.route(server, "server/routes");
// register routes passing in a specific connection and a config
// ensure the locations are not clashing with the above...
routing.route(server, "server/more-routes", mongoose.createConnection(config.db), config);

Using the module as an express middleware is currently under development and will be added shortly:

server.use(routing.route("server/routes"));

License

The MIT License

Copyright (c) 2014 Oleg Sklyar <osklyar@qucado.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.