Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced all tabs with spaces and fixed some alignment
- Loading branch information
Showing
7 changed files
with
336 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,51 +1,51 @@ | |||
/* | /* | ||
* This is an example of how to use Ni to organize your code into a nice, | * This is an example of how to use Ni to organize your code into a nice, | ||
* neat MVC project. | * neat MVC project. | ||
* | * | ||
* You can place your controllers, models, views, libraries and helpers into | * You can place your controllers, models, views, libraries and helpers into | ||
* respective folders /controllers, /models, /views, /libraries, /helpers, and | * respective folders /controllers, /models, /views, /libraries, /helpers, and | ||
* they will be loaded when you call Ni.boot into the Ni object. | * 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 | * Take a look at the example controllers and views for how to structure that | ||
* code to make it integrate with Ni. | * code to make it integrate with Ni. | ||
*/ | */ | ||
|
|
||
/* | /* | ||
* Module dependencies | * Module dependencies | ||
*/ | */ | ||
|
|
||
var Connect = require('connect'), | var Connect = require('connect'), | ||
Quip = require('quip'), | Quip = require('quip'), | ||
Ni = require('../lib/ni'); | 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.setRoot(__dirname); // Tells Ni where to look for the folders | ||
Ni.config.location = 'world'; // Sets a custom configuration variable to use | Ni.config.location = 'world'; // Sets a custom configuration variable to use | ||
// later, anywhere | // later, anywhere | ||
|
|
||
Ni.boot(function() { // Boots Ni and loads everything | Ni.boot(function() { // Boots Ni and loads everything | ||
var app = Connect.createServer( // Create server when Ni is finished | var app = Connect.createServer( // Create server when Ni is finished | ||
// booting | // booting | ||
Quip(), // Helps in sending HTTP responses | Quip(), // Helps in sending HTTP responses | ||
Ni.router, // The Ni router automatically | Ni.router, // The Ni router automatically | ||
// directs requests based on URL | // directs requests based on URL | ||
// segments to the appropriate | // segments to the appropriate | ||
// controller functions | // controller functions | ||
function (req, res, next) { // Called if no controller / | function (req, res, next) { // Called if no controller / | ||
// function for the URL given is | // function for the URL given is | ||
// found | // found | ||
res.notFound('Page not found.'); | res.notFound('Page not found.'); | ||
} | } | ||
); | ); | ||
app.listen(3000); | app.listen(3000); | ||
|
|
||
console.log('Application server started on port 3000'); | console.log('Application server started on port 3000'); | ||
}); | }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,73 +1,73 @@ | |||
/* | /* | ||
* CalculatorController - a controller to be used with Ni that is called by | * CalculatorController - a controller to be used with Ni that is called by | ||
* the router when a user visits a URL that starts with /calculator. | * the router when a user visits a URL that starts with /calculator. | ||
* | * | ||
* Controllers are represented as Javascript objects, and exported as a Node | * Controllers are represented as Javascript objects, and exported as a Node | ||
* module after the object definition. | * module after the object definition. | ||
* | * | ||
* Functions in controllers are called with the request and result as arguments | * 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 | * (just like Connect functions are called) and then with the rest of the | ||
* segments of the URL as the rest of the arguments. | * segments of the URL as the rest of the arguments. | ||
* | * | ||
* For example, loading the URL /calculator/add/4/5 would call the add function | * For example, loading the URL /calculator/add/4/5 would call the add function | ||
* below with a = 4 and b = 5. | * below with a = 4 and b = 5. | ||
* | * | ||
* If no function is specified in the URL (such as the URL /calculator), then | * If no function is specified in the URL (such as the URL /calculator), then | ||
* then index function is called if it exists. | * then index function is called if it exists. | ||
* | * | ||
* Note: The req argument gives you access to the request, and you can use the | * 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. | * res argument to send back a response to the browser or requester. | ||
*/ | */ | ||
|
|
||
/* | /* | ||
* Module dependencies | * Module dependencies | ||
*/ | */ | ||
|
|
||
var Ni = require('../../lib/ni'), | var Ni = require('../../lib/ni'), | ||
Mu = require('mu'), | Mu = require('mu'), | ||
Quip = require('quip'); | Quip = require('quip'); | ||
|
|
||
/* | /* | ||
* The calculator controller | * The calculator controller | ||
*/ | */ | ||
|
|
||
var CalculatorController = function() { | var CalculatorController = function() { | ||
/* | /* | ||
* This function is called when the URL does not indicate a function to | * This function is called when the URL does not indicate a function to | ||
* be called, so it would look like /calculator. | * be called, so it would look like /calculator. | ||
*/ | */ | ||
|
|
||
this.index = function(req, res) { | this.index = function(req, res) { | ||
res.ok('Welcome to the calculator!'); | res.ok('Welcome to the calculator!'); | ||
} | } | ||
|
|
||
/* | /* | ||
* This function is called when the URL indicates "add" as the function | * This function is called when the URL indicates "add" as the function | ||
* to be called, so it would look like /calculator/add. | * to be called, so it would look like /calculator/add. | ||
*/ | */ | ||
this.add = function(req, res, a, b) { | this.add = function(req, res, a, b) { | ||
if (a && b) { | if (a && b) { | ||
a = parseInt(a); | a = parseInt(a); | ||
b = parseInt(b); | b = parseInt(b); | ||
|
|
||
var template = Ni.views.calculator.template; | var template = Ni.views.calculator.template; | ||
var data = {result: a + b}; | var data = {result: a + b}; | ||
|
|
||
var compiled = Mu.compileText(template, null); | var compiled = Mu.compileText(template, null); | ||
compiled(data).addListener('data', function (c) { | compiled(data).addListener('data', function (c) { | ||
res.ok(c); | res.ok(c); | ||
}); | }); | ||
} | } | ||
else { | else { | ||
res.error("a and b must both be provided."); | res.error("a and b must both be provided."); | ||
} | } | ||
} | } | ||
}; | }; | ||
|
|
||
/* | /* | ||
* Exports the calculator controller | * Exports the calculator controller | ||
*/ | */ | ||
|
|
||
module.exports = new CalculatorController(); | module.exports = new CalculatorController(); |
Oops, something went wrong.