-
Notifications
You must be signed in to change notification settings - Fork 10
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 | 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, | ||
* 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'); | ||
}); |
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 | Diff line number | Diff line change |
---|---|---|
@@ -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(); |
Oops, something went wrong.