Permalink
Browse files

Published to NPM, made changes to examples, more documentation.

  • Loading branch information...
kishorenc committed Nov 14, 2011
1 parent 4c2cc57 commit cff07e344669b946333c9d48d25bf39547e83a69
Showing with 70 additions and 94 deletions.
  1. +1 −0 .npmignore
  2. +16 −4 README.md
  3. +9 −5 example/app.js
  4. +10 −0 example/controllers/indexController.js
  5. +0 −78 example/road.js
  6. +1 −1 example/routes.js
  7. +1 −0 index.js
  8. +6 −6 lib/road.js
  9. +26 −0 package.json
View
@@ -0,0 +1 @@
+examples
View
@@ -1,11 +1,23 @@
-#ROAD
+#Road
-A route helper for expressjs that allows you to have specific controllers mapping to their corresponding views by following logical conventions. See example application for use cases. You need express, and ejs available to run the example.
+A route helper for express that allows you to have map routes, controllers and views by following logical conventions. See example application for use cases. You need express, and ejs available to run the example.
-##TODO
+##Installation
- * NPM package
+ npm install road
+##Features
+
+ * Wire your routes to individual controllers and views
+ * Define custom routes which map custom URLs to specific controller methods
+ * Serve the views in custom MIME types
+
+See example application for complete use cases.
+
+##Todo
+
+ * Better documentation (though example app covers ample ground)
+ * Tests
##License
View
@@ -1,18 +1,22 @@
/*
* Sample app showcasing road
-* Make sure you have express, and ejs installed to run the example
+* Make sure you have express, ejs, and (of course) road installed to run the example
*/
-var express = require('express')
- ,road = require('./road');
+var express = require('express'),
+ app = express.createServer();
-var app = express.createServer();
+var road = require('road');
+
+// configure road by specifying: the view engine, application root, and the routes (see routes.js)
+road.configure('ejs', __dirname, require('./routes'));
-road.configure('ejs','routes');
app.configure(function() {
app.use(express.static(__dirname+'/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
+
+ // use road to handle the routes this way:
app.use(express.router(road.router));
});
@@ -1,3 +1,8 @@
+/*
+* Each controller method recieves `conn` and `callback` as parameter
+* 1. `conn` contains request and response objects, which can be accessed like `conn.req` or `conn.res`
+* 2. `callback` method is used to specify the view to be rendered, see examples below
+*/
// serving a static view
this.index = function(conn, callback) {
@@ -23,3 +28,8 @@ this.plainText = function(conn, callback) {
this.plainTextAsJS = function(conn, callback) {
callback('var foo = "bar";', 'application/javascript');
}
+
+// custom URL mapping
+this.custom = function(conn, callback) {
+ callback('custom URL mapping');
+}
View
@@ -1,78 +0,0 @@
-var routes = require('./routes'),
- fs = require('fs');
-
-var config = {};
-
-this.configure = function (viewExtension, routesPath) {
- config.viewExtension = viewExtension;
- config.routes = require('./' + routesPath);
-}
-
-this.router = function (app) {
- for (var index in config.routes) {
- var route = routes[index];
- if (route[0] == 'map') {
- // custom routes
- var method = route[1],
- path = route[2],
- controllerName = route[3],
- actionName = route[4];
- app[method].apply(null, [path, mappedResponse(controllerName, actionName)]);
- } else {
- // default routes
- var method = route[0],
- path = route[1];
- app[method].apply(null, [path, function (req, res) {
- var controller = req.params.controller || "index";
- var action = req.params.action || "index";
- callController(controller, action, req, res);
- }]);
- }
- }
-};
-
-function mappedResponse(controllerName, actionName) {
- return function (req, res) {
- callController(controllerName, actionName, req, res);
- };
-}
-
-function callController(controllerName, functionName, req, res) {
- var controller = require('./controllers/' + controllerName + "Controller");
- var connection = {
- 'req': req,
- 'res': res
- };
- controller[functionName].apply(null, [connection, createControllerCallback(controllerName, req, res)]);
-}
-
-function createControllerCallback(controllerName, req, res) {
- return function (viewReturned, contentType) {
- if (typeof (viewReturned) == 'object' && !(viewReturned instanceof Array)) {
- console.error("Road.js - exception on page:" + req.url);
- } else {
- renderView(controllerName, viewReturned, contentType, req, res);
- }
- }
-}
-
-function renderView(controllerName, viewOptions, contentType, req, res) {
- if (viewOptions.constructor === Array) {
- // viewOptions = [ viewFile, [,data] ]
- var cType = contentType || 'text/html';
- res.header("Content-Type", cType);
-
- if (!viewOptions[1]) viewOptions[1] = {};
- viewOptions[1].layout = false;
-
- var viewFile = controllerName + '/' + viewOptions[0] + '.' + config.viewExtension;
- res.render(viewFile, viewOptions[1]);
- } else {
- // plain string passed
- var cType = contentType || 'text/plain';
- res.writeHead(200, {
- 'Content-Type': cType
- });
- res.end(viewOptions);
- }
-}
View
@@ -1,6 +1,6 @@
module.exports = [
// custom routes
- ['map', 'get', '/testControllerShowMethod', 'test', 'show'],
+ ['map', 'get', '/testControllerShowMethod', 'index', 'custom'],
// default route
['all', '/:controller?/:action?/:id?']
View
@@ -0,0 +1 @@
+module.exports = require('./lib/road');
View
@@ -1,16 +1,16 @@
-var routes = require('./routes'),
- fs = require('fs');
+var fs = require('fs');
var config = {};
-this.configure = function (viewExtension, routesPath) {
+this.configure = function (viewExtension, appRoot, routes) {
config.viewExtension = viewExtension;
- config.routes = require('./' + routesPath);
+ config.appRoot = appRoot;
+ config.routes = routes || [];
}
this.router = function (app) {
for (var index in config.routes) {
- var route = routes[index];
+ var route = config.routes[index];
if (route[0] == 'map') {
// custom routes
var method = route[1],
@@ -38,7 +38,7 @@ function mappedResponse(controllerName, actionName) {
}
function callController(controllerName, functionName, req, res) {
- var controller = require('./controllers/' + controllerName + "Controller");
+ var controller = require(config.appRoot+'/controllers/' + controllerName + "Controller");
var connection = {
'req': req,
'res': res
View
@@ -0,0 +1,26 @@
+{
+ "name": "road",
+ "version": "0.0.2",
+ "description": "A route helper for express.",
+ "homepage": "https://github.com/kishorenc/road",
+ "author": "Kishore Nallan <kishore@kishorelive.com>",
+ "contributors": [
+ "Kishore Nallan <kishore@kishorelive.com>"
+ ],
+ "repository":
+ {
+ "type": "git",
+ "url": "https://github.com/kishorenc/road"
+ },
+ "bugs":
+ {
+ "mail": "kishore@kishorelive.com",
+ "url": "https://github.com/kishorenc/road/issues"
+ },
+ "main": "index.js",
+ "engines": { "node": ">=0.4.0" },
+ "licenses" :
+ [
+ { "type" : "MIT", "url" : "https://raw.github.com/kishorenc/road/master/LICENSE" }
+ ]
+}

0 comments on commit cff07e3

Please sign in to comment.