Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Replaced all tabs with spaces and fixed some alignment

  • Loading branch information...
commit d03cf0a16e9b5a4c86b3cad5e33adcad11bf5034 1 parent 9724942
@chetan51 authored
View
124 README.md
@@ -17,11 +17,11 @@ How do I use Ni?
It's as simple as telling Ni where to look for your files, and then asking it to boot:
- var Ni = require('../lib/ni');
- Ni.setRoot(__dirname);
- Ni.boot(function() {
- // Ready to start the server!
- }
+ var Ni = require('../lib/ni');
+ Ni.setRoot(__dirname);
+ Ni.boot(function() {
+ // Ready to start the server!
+ }
The rest of your code now has access to all your models, views, and controllers in `Ni.models`, `Ni.views` and `Ni.controllers`.
@@ -34,14 +34,14 @@ Ni provides a router you can use with Connect to have requests sent to the appro
Use it with Connect:
- var app = Connect.createServer(
- Ni.router
- // You can add other Connect middle-ware here
- );
+ var app = Connect.createServer(
+ Ni.router
+ // You can add other Connect middle-ware here
+ );
It parses the URL and sends the request to the correct controller function as follows:
- http://yourapp.com/[controller]/[function]/[argument 1]/[argument 2]/[etc]
+ http://yourapp.com/[controller]/[function]/[argument 1]/[argument 2]/[etc]
If no controller is specified (`http://yourapp.com/`), it loads the `home` controller's `index` function.
@@ -52,15 +52,15 @@ Can I see an example?
If you have your project organized like this:
- /app.js
- /controllers
- /calculator.js
- /home.js
- /views
- /calculator.html
- /home.html
- /models
- /calculator.js
+ /app.js
+ /controllers
+ /calculator.js
+ /home.js
+ /views
+ /calculator.html
+ /home.html
+ /models
+ /calculator.js
You can access your stuff with:
@@ -75,49 +75,49 @@ How would my controllers, models, libraries, helpers look?
Each of those is just a Node module. For example, the calculator controller mentioned above (in the `/example` folder) looks like this:
- var Ni = require('../../lib/ni'),
- Mu = require('mu'),
- Quip = require('quip');
-
- var CalculatorController = function() {
-
- /*
- * This function is called when the URL does not indicate a function to
- * be called, so it would look like /calculator.
- */
-
- this.index = function(req, res) {
- res.ok('Welcome to the calculator!');
- }
-
- /*
- * This function is called when the URL indicates "add" as the function
- * to be called, so it would look like /calculator/add.
- *
- * For example, loading the URL /calculator/add/4/5 would call the below
- * function with a = 4 and b = 5.
- */
-
- this.add = function(req, res, a, b) {
- if (a && b) {
- a = parseInt(a);
- b = parseInt(b);
-
- var template = Ni.views.calculator.template;
- var data = {result: a + b};
-
- var compiled = Mu.compileText(template, null);
- compiled(data).addListener('data', function (c) {
- res.ok(c);
- });
- }
- else {
- res.error("a and b must both be provided.");
- }
- }
- };
-
- module.exports = new CalculatorController();
+ var Ni = require('../../lib/ni'),
+ Mu = require('mu'),
+ Quip = require('quip');
+
+ var CalculatorController = function() {
+
+ /*
+ * This function is called when the URL does not indicate a function to
+ * be called, so it would look like /calculator.
+ */
+
+ this.index = function(req, res) {
+ res.ok('Welcome to the calculator!');
+ }
+
+ /*
+ * This function is called when the URL indicates "add" as the function
+ * to be called, so it would look like /calculator/add.
+ *
+ * For example, loading the URL /calculator/add/4/5 would call the below
+ * function with a = 4 and b = 5.
+ */
+
+ this.add = function(req, res, a, b) {
+ if (a && b) {
+ a = parseInt(a);
+ b = parseInt(b);
+
+ var template = Ni.views.calculator.template;
+ var data = {result: a + b};
+
+ var compiled = Mu.compileText(template, null);
+ compiled(data).addListener('data', function (c) {
+ res.ok(c);
+ });
+ }
+ else {
+ res.error("a and b must both be provided.");
+ }
+ }
+ };
+
+ module.exports = new CalculatorController();
How to organize your code
-------------------------
View
72 example/app.js
@@ -1,51 +1,51 @@
-/*
- * This is an example of how to use Ni to organize your code into a nice,
- * neat MVC project.
+/*
+ * This is an example of how to use Ni to organize your code into a nice,
+ * neat MVC project.
*
- * You can place your controllers, models, views, libraries and helpers into
- * respective folders /controllers, /models, /views, /libraries, /helpers, and
- * they will be loaded when you call Ni.boot into the Ni object.
+ * You can place your controllers, models, views, libraries and helpers into
+ * respective folders /controllers, /models, /views, /libraries, /helpers, and
+ * they will be loaded when you call Ni.boot into the Ni object.
*
- * Take a look at the example controllers and views for how to structure that
- * code to make it integrate with Ni.
+ * Take a look at the example controllers and views for how to structure that
+ * code to make it integrate with Ni.
*/
/*
- * Module dependencies
+ * Module dependencies
*/
var Connect = require('connect'),
- Quip = require('quip'),
- Ni = require('../lib/ni');
+ Quip = require('quip'),
+ Ni = require('../lib/ni');
/*
- * Load Ni and start the server.
+ * Load Ni and start the server.
*/
-Ni.setRoot(__dirname); // Tells Ni where to look for the folders
-Ni.config.location = 'world'; // Sets a custom configuration variable to use
- // later, anywhere
+Ni.setRoot(__dirname); // Tells Ni where to look for the folders
+Ni.config.location = 'world'; // Sets a custom configuration variable to use
+ // later, anywhere
-Ni.boot(function() { // Boots Ni and loads everything
-
- var app = Connect.createServer( // Create server when Ni is finished
- // booting
-
- Quip(), // Helps in sending HTTP responses
-
- Ni.router, // The Ni router automatically
- // directs requests based on URL
- // segments to the appropriate
- // controller functions
-
- function (req, res, next) { // Called if no controller /
- // function for the URL given is
- // found
- res.notFound('Page not found.');
- }
- );
-
- app.listen(3000);
+Ni.boot(function() { // Boots Ni and loads everything
+
+ var app = Connect.createServer( // Create server when Ni is finished
+ // booting
+
+ Quip(), // Helps in sending HTTP responses
+
+ Ni.router, // The Ni router automatically
+ // directs requests based on URL
+ // segments to the appropriate
+ // controller functions
+
+ function (req, res, next) { // Called if no controller /
+ // function for the URL given is
+ // found
+ res.notFound('Page not found.');
+ }
+ );
+
+ app.listen(3000);
- console.log('Application server started on port 3000');
+ console.log('Application server started on port 3000');
});
View
94 example/controllers/calculator.js
@@ -1,73 +1,73 @@
/*
- * CalculatorController - a controller to be used with Ni that is called by
- * the router when a user visits a URL that starts with /calculator.
+ * CalculatorController - a controller to be used with Ni that is called by
+ * the router when a user visits a URL that starts with /calculator.
*
- * Controllers are represented as Javascript objects, and exported as a Node
- * module after the object definition.
- *
- * Functions in controllers are called with the request and result as arguments
- * (just like Connect functions are called) and then with the rest of the
- * segments of the URL as the rest of the arguments.
+ * Controllers are represented as Javascript objects, and exported as a Node
+ * module after the object definition.
+ *
+ * Functions in controllers are called with the request and result as arguments
+ * (just like Connect functions are called) and then with the rest of the
+ * segments of the URL as the rest of the arguments.
*
- * For example, loading the URL /calculator/add/4/5 would call the add function
- * below with a = 4 and b = 5.
+ * For example, loading the URL /calculator/add/4/5 would call the add function
+ * below with a = 4 and b = 5.
*
- * If no function is specified in the URL (such as the URL /calculator), then
- * then index function is called if it exists.
+ * If no function is specified in the URL (such as the URL /calculator), then
+ * then index function is called if it exists.
*
- * Note: The req argument gives you access to the request, and you can use the
- * res argument to send back a response to the browser or requester.
+ * Note: The req argument gives you access to the request, and you can use the
+ * res argument to send back a response to the browser or requester.
*/
/*
- * Module dependencies
+ * Module dependencies
*/
var Ni = require('../../lib/ni'),
- Mu = require('mu'),
- Quip = require('quip');
+ Mu = require('mu'),
+ Quip = require('quip');
/*
- * The calculator controller
+ * The calculator controller
*/
var CalculatorController = function() {
-
- /*
- * This function is called when the URL does not indicate a function to
- * be called, so it would look like /calculator.
- */
+
+ /*
+ * This function is called when the URL does not indicate a function to
+ * be called, so it would look like /calculator.
+ */
- this.index = function(req, res) {
- res.ok('Welcome to the calculator!');
- }
+ this.index = function(req, res) {
+ res.ok('Welcome to the calculator!');
+ }
- /*
- * This function is called when the URL indicates "add" as the function
- * to be called, so it would look like /calculator/add.
- */
-
- this.add = function(req, res, a, b) {
- if (a && b) {
- a = parseInt(a);
- b = parseInt(b);
+ /*
+ * This function is called when the URL indicates "add" as the function
+ * to be called, so it would look like /calculator/add.
+ */
+
+ this.add = function(req, res, a, b) {
+ if (a && b) {
+ a = parseInt(a);
+ b = parseInt(b);
- var template = Ni.views.calculator.template;
- var data = {result: a + b};
+ var template = Ni.views.calculator.template;
+ var data = {result: a + b};
- var compiled = Mu.compileText(template, null);
- compiled(data).addListener('data', function (c) {
- res.ok(c);
- });
- }
- else {
- res.error("a and b must both be provided.");
- }
- }
+ var compiled = Mu.compileText(template, null);
+ compiled(data).addListener('data', function (c) {
+ res.ok(c);
+ });
+ }
+ else {
+ res.error("a and b must both be provided.");
+ }
+ }
};
/*
- * Exports the calculator controller
+ * Exports the calculator controller
*/
module.exports = new CalculatorController();
View
20 example/controllers/home.js
@@ -1,24 +1,28 @@
/*
- * HomeController - a controller to be used with Ni that is called by
- * the router when a user visits the root URL, /.
+ * HomeController - a controller to be used with Ni that is called by
+ * the router when a user visits the root URL, /.
*
- * When the root URL, /, is loaded, the index function below is called.
+ * When the root URL, /, is loaded, the index function below is called.
*/
/*
- * The home controller
+ * Module dependencies
*/
+
var Ni = require('../../lib/ni');
+/*
+ * The home controller
+ */
var HomeController = function() {
- this.index = function(req, res) {
- res.ok('Hello ' + Ni.config.location + '!');
- }
+ this.index = function(req, res) {
+ res.ok('Hello ' + Ni.config.location + '!');
+ }
};
/*
- * Exports the home controller
+ * Exports the home controller
*/
module.exports = new HomeController();
View
18 example/views/calculator.html
@@ -1,14 +1,14 @@
<!--
- * Views are loaded as plain text files so that you can use your own template
- * parser with them, or just put HTML code into them and use them directly.
+ * Views are loaded as plain text files so that you can use your own template
+ * parser with them, or just put HTML code into them and use them directly.
*
- * For example, this view uses the Mu template engine, and the template code
- * below can be accessed at Ni.views.calculator.template.
- *
- * Note: Make sure that you have unique names for your files, regardless of
- * file extension. For example, you should have just calculator.html, and not
- * calculator.html and calculator.js. That way, Ni.views.calculator will
- * point to one definite file.
+ * For example, this view uses the Mu template engine, and the template code
+ * below can be accessed at Ni.views.calculator.template.
+ *
+ * Note: Make sure that you have unique names for your files, regardless of
+ * file extension. For example, you should have just calculator.html, and not
+ * calculator.html and calculator.js. That way, Ni.views.calculator will
+ * point to one definite file.
*/
-->
View
328 lib/ni.js
@@ -1,225 +1,225 @@
/*
- * Ni - a minimalistic Node framework that makes setting up an MVC project
- * a breeze. Inspired by CodeIgniter.
+ * Ni - a minimalistic Node framework that makes setting up an MVC project
+ * a breeze. Inspired by CodeIgniter.
*
- * Since Ni is fully contained in a Node module, you can use it with Connect
- * and your own choice of template engine, database, static file server, etc.
- * No frills, no restrictions, no worries.
+ * Since Ni is fully contained in a Node module, you can use it with Connect
+ * and your own choice of template engine, database, static file server, etc.
+ * No frills, no restrictions, no worries.
*/
/*
- * Module dependencies
+ * Module dependencies
*/
var fs = require('fs'),
- Step = require('step');
+ Step = require('step');
/*
- * Ni object
+ * Ni object
*/
var Ni = function() {
- var Ni = this;
-
- /*
- * Registry for custom configuration variables.
- */
-
- this.config = {};
-
- /*
- * Tells Ni what root directory to look into.
- *
- * Call this function before calling boot.
- */
-
- this.setRoot = function(__root) {
- this.__root = __root;
- }
-
- /*
- * Loads controllers, models, views, templates and helpers from the root
- * directory, and makes them all available to the Ni object.
- */
-
- this.boot = function(callbackDone) {
- Step(
- function bootStrapEverything() {
- bootStrapControllers(Ni.__root, this.parallel());
- bootStrapModels(Ni.__root, this.parallel());
- bootStrapViews(Ni.__root, this.parallel());
- bootStrapLibraries(Ni.__root, this.parallel());
- },
- function buildNi(err, controllers, models, views, libraries) {
- if (err) throw err;
- Ni.controllers = controllers;
- Ni.models = models;
- Ni.views = views;
- Ni.libraries = libraries;
- callbackDone();
- }
- );
- }
-
- /*
- * Provides router for Connect to automatically call the correct controller
- * functions with the correct arguments based on the URL. If the controller
- * or action is not found, it calls next() in the Connect list.
- *
- * Add this to your Connect call as: Ni.router
- */
-
- this.router = function(req, res, next) {
- parsedUrl = require('url').parse(req.url, true);
- pathArr = parsedUrl.pathname.split('/');
-
- var controller;
- if (pathArr[1]) {
- controller = Ni.controllers[pathArr[1]];
- if (controller) {
- var fn;
- if (pathArr[2]) {
- fn = controller[pathArr[2]];
- }
- else {
- fn = controller.index;
- }
-
- if (fn) {
- var args = pathArr.slice(3);
- args.unshift(res);
- args.unshift(req);
- fn.apply(null, args);
- }
- else {
- next();
- }
- }
- else {
- next();
- }
- }
- else {
- controller = Ni.controllers.home;
- if (controller && controller.index) {
- controller.index(req, res);
- }
- else {
- next();
- }
- }
- }
+ var Ni = this;
+
+ /*
+ * Registry for custom configuration variables.
+ */
+
+ this.config = {};
+
+ /*
+ * Tells Ni what root directory to look into.
+ *
+ * Call this function before calling boot.
+ */
+
+ this.setRoot = function(__root) {
+ this.__root = __root;
+ }
+
+ /*
+ * Loads controllers, models, views, templates and helpers from the root
+ * directory, and makes them all available to the Ni object.
+ */
+
+ this.boot = function(callbackDone) {
+ Step(
+ function bootStrapEverything() {
+ bootStrapControllers(Ni.__root, this.parallel());
+ bootStrapModels(Ni.__root, this.parallel());
+ bootStrapViews(Ni.__root, this.parallel());
+ bootStrapLibraries(Ni.__root, this.parallel());
+ },
+ function buildNi(err, controllers, models, views, libraries) {
+ if (err) throw err;
+ Ni.controllers = controllers;
+ Ni.models = models;
+ Ni.views = views;
+ Ni.libraries = libraries;
+ callbackDone();
+ }
+ );
+ }
+
+ /*
+ * Provides router for Connect to automatically call the correct controller
+ * functions with the correct arguments based on the URL. If the controller
+ * or action is not found, it calls next() in the Connect list.
+ *
+ * Add this to your Connect call as: Ni.router
+ */
+
+ this.router = function(req, res, next) {
+ parsedUrl = require('url').parse(req.url, true);
+ pathArr = parsedUrl.pathname.split('/');
+
+ var controller;
+ if (pathArr[1]) {
+ controller = Ni.controllers[pathArr[1]];
+ if (controller) {
+ var fn;
+ if (pathArr[2]) {
+ fn = controller[pathArr[2]];
+ }
+ else {
+ fn = controller.index;
+ }
+
+ if (fn) {
+ var args = pathArr.slice(3);
+ args.unshift(res);
+ args.unshift(req);
+ fn.apply(null, args);
+ }
+ else {
+ next();
+ }
+ }
+ else {
+ next();
+ }
+ }
+ else {
+ controller = Ni.controllers.home;
+ if (controller && controller.index) {
+ controller.index(req, res);
+ }
+ else {
+ next();
+ }
+ }
+ }
};
/* Helper functions
*******************************************************************************/
/*
- * Loads controllers.
+ * Loads controllers.
*/
function bootStrapControllers(__root, callback) {
- bootStrapModules(__root + '/controllers', function(err, controllers) {
- if (err) throw err;
- callback(null, controllers);
- });
+ bootStrapModules(__root + '/controllers', function(err, controllers) {
+ if (err) throw err;
+ callback(null, controllers);
+ });
}
/*
- * Loads models.
+ * Loads models.
*/
function bootStrapModels(__root, callback) {
- bootStrapModules(__root + '/models', function(err, models) {
- if (err) throw err;
- callback(null, models);
- });
+ bootStrapModules(__root + '/models', function(err, models) {
+ if (err) throw err;
+ callback(null, models);
+ });
}
/*
- * Loads views.
+ * Loads views.
*/
function bootStrapViews(__root, callback) {
- var __views = __root + '/views';
- var views = {};
-
- loadFilesFromDir(__views, function(dir, file, name) {
- var view = {};
- view.path = dir + '/' + file;
- fs.readFile(view.path, 'utf-8', function(err, data) {
- view.template = data;
- views[name] = view;
- });
- }, function(err) {
- if (err) throw err;
- callback(null, views);
- });
+ var __views = __root + '/views';
+ var views = {};
+
+ loadFilesFromDir(__views, function(dir, file, name) {
+ var view = {};
+ view.path = dir + '/' + file;
+ fs.readFile(view.path, 'utf-8', function(err, data) {
+ view.template = data;
+ views[name] = view;
+ });
+ }, function(err) {
+ if (err) throw err;
+ callback(null, views);
+ });
}
/*
- * Loads libraries.
+ * Loads libraries.
*/
function bootStrapLibraries(__root, callback) {
- bootStrapModules(__root + '/libraries', function(err, libraries) {
- if (err) throw err;
- callback(null, libraries);
- });
+ bootStrapModules(__root + '/libraries', function(err, libraries) {
+ if (err) throw err;
+ callback(null, libraries);
+ });
}
/*
- * Loads anything that is represented as a Node.js module.
- * This includes controllers, models, libraries and helpers.
+ * Loads anything that is represented as a Node.js module.
+ * This includes controllers, models, libraries and helpers.
*/
function bootStrapModules(__root, callback) {
- var __modules = __root;
- var modules = {};
-
- loadFilesFromDir(__modules, function(dir, file, name) {
- var name = file.split('.')[0];
- modules[name] = require(dir + '/' + file);
- }, function(err) {
- if (err) callback(err);
- else callback(null, modules);
- });
+ var __modules = __root;
+ var modules = {};
+
+ loadFilesFromDir(__modules, function(dir, file, name) {
+ var name = file.split('.')[0];
+ modules[name] = require(dir + '/' + file);
+ }, function(err) {
+ if (err) callback(err);
+ else callback(null, modules);
+ });
}
/*
- * Loads files from given directory and calls the given function on them.
+ * Loads files from given directory and calls the given function on them.
*/
function loadFilesFromDir(dir, fnOnFile, callbackDone) {
fs.readdir(dir, function(err, files){
if (err || !files) callbackDone(null);
- else {
- Step(
- function readFiles() {
- if (files.length > 0) {
- var group = this.group();
- files.forEach(function (file){
- var name = file.split('.')[0];
-
- fnOnFile(dir, file, name);
- group()();
- });
- }
- else {
- return null;
- }
- },
- function finish(err) {
- if (err) callbackDone(err);
- callbackDone(null);
- }
- );
- }
+ else {
+ Step(
+ function readFiles() {
+ if (files.length > 0) {
+ var group = this.group();
+ files.forEach(function (file){
+ var name = file.split('.')[0];
+
+ fnOnFile(dir, file, name);
+ group()();
+ });
+ }
+ else {
+ return null;
+ }
+ },
+ function finish(err) {
+ if (err) callbackDone(err);
+ callbackDone(null);
+ }
+ );
+ }
});
}
/*
- * Exports the Ni object to Node.js.
+ * Exports the Ni object to Node.js.
*/
module.exports = new Ni();
View
12 package.json
@@ -13,10 +13,10 @@
},
"description": "A minimalistic Node module / framework that makes setting up an MVC project a breeze",
"author": "Chetan Surpur <chetan.surpur@gmail.com>",
- "homepage": "http://github.com/chetan51/ni",
- "repository" :
- {
- "type" : "git",
- "url" : "git@github.com:chetan51/ni.git"
- }
+ "homepage": "http://github.com/chetan51/ni",
+ "repository" :
+ {
+ "type" : "git",
+ "url" : "git@github.com:chetan51/ni.git"
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.