Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit f154f5db44022213d6d238d6b9309d40192e7fc4 @chetan51 committed Oct 3, 2010
Showing with 503 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +121 −0 README.md
  3. +55 −0 example/app.js
  4. +57 −0 example/controllers/calculator.js
  5. +14 −0 example/controllers/home.js
  6. +15 −0 example/views/calculator.html
  7. +219 −0 lib/ni.js
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2010 Chetan Surpur.
+
+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.
121 README.md
@@ -0,0 +1,121 @@
+Ni - a minimalistic Node framework that makes setting up an MVC project a breeze
+================================================================================
+
+Ni helps you set up a well-organized project, with a file structure separated into models, views, controllers, libraries and helpers. It's intuitive and simple to use, and doesn't get in the way of using other modules and Node plugins in your project.
+
+Ni is inspired by [CodeIgniter](http://codeigniter.com/), and it's name is an abbreviation of NodeIgniter.
+
+What's awesome about Ni?
+---------------------------
+
+* It's packaged as a regular Node module, so you just "require" it and you're ready to go
+* You can use other Node modules and Connect middle-ware as you usually would; Ni does not get in the way
+* It's easy to use and loads your models, views, controllers, libraries and helpers automatically so you can just start using them everywhere
+
+How do I use Ni?
+-------------------
+
+It's as simple as telling Ni where to look for your files, and then asking it to boot:
+
+ global.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.
+
+Note that your controllers, models, libraries and helpers should be packaged as Node modules, and your views can be plain text or html files.
+
+You even get a router for free!
+-------------------------------
+
+Ni provides a router you can use with Connect to have requests sent to the appropriate controller functions according to URL segments.
+
+Use it with Connect:
+
+ 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]
+
+If no controller is specified (http://yourapp.com/), it loads the "home" controller's "index" function.
+
+If no function is specified (http://yourapp.com/[controller]), it loads the [controller]'s "index" function.
+
+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
+
+You can access your stuff with:
+
+* Ni.controllers.calculator
+* Ni.views.calculator
+* Ni.models.calculator
+
+A really well-commented example is in the source code in the /example folder, check it out!
+
+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 /examples folder) looks like this:
+
+ 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();
+
+Let's get cracking!
+-------------------
+
+Install Ni as your would any other Node module, by copying lib/ni.js to your ~/.node_libraries folder. Then, copy the /example directory and modify it to set up your project. Run "node app.js" in the copied directory to start the server.
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ *
+ * Take a look at the example controllers and views for how to structure that
+ * code to make it integrate with Ni.
+ */
+
+/*
+ * Module dependencies
+ */
+
+global.Connect = require('connect');
+global.Mu = require('mu');
+global.Quip = require('quip');
+
+/*
+ * Initialize Ni into a global object.
+ */
+
+global.Ni = require('../lib/ni');
+
+/*
+ * Load Ni and start the server.
+ */
+
+Ni.setRoot(__dirname); // Tells Ni where to look for the folders
+
+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');
+});
@@ -0,0 +1,57 @@
+/*
+ * 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.
+ *
+ * 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.
+ *
+ * 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.
+ */
+
+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.
+ */
+
+ 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();
@@ -0,0 +1,14 @@
+/*
+ * HomeControllers - 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.
+ */
+
+var HomeController = function() {
+ this.index = function(req, res) {
+ res.ok('Hello world!');
+ }
+};
+
+module.exports = new HomeController();
@@ -0,0 +1,15 @@
+<!--
+ * 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.
+ */
+-->
+
+Result: <b>{{result}}</b>
Oops, something went wrong.

0 comments on commit f154f5d

Please sign in to comment.