• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 14 years ago
  • Updated over 12 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A minimalistic Node module / framework that makes setting up an MVC project a walk in the park

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, and its 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 doesn't 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:

var Ni = require('../lib/ni');

Ni.config('root', "myapp/src");

Ni.boot(function() {
    // Ready to start the server!
});

The rest of your code now has access to all your models, views, and controllers with Ni.model('MODELNAME'), Ni.view('VIEWNAME') and Ni.controller('CONTROLLERNAME').

Note that your controllers, models, libraries and helpers should be packaged as Node modules, and your views can be plain text, template (Markdown, Mustache, etc), 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://myapp.com/[controller]/[function]/[argument 1]/[argument 2]/[etc]

If no controller is specified (http://myapp.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.

You can define custom routes using Ni.addRoute(source, destination, [method])

Some examples:

With this custom route calling myapp.com will internally redirect to use your News controller and call the index function on it:

Ni.addRoute('/', '/News/index');

You can use regular expressions as well. This leads myapp.com/register to your User controller and its register function:

Ni.addRoute(/^\/register/i, '/User/register');

If you want to use arguments with custom routes, you can do that as well:

Ni.addRoute(/^\/details\/(.*)$/i, '/User/details/$1');

You can also define functions to test the path. For example:

Calling myapp.com/add/1/2 will internally redirect to use the Number controller and call the positive function, while calling myapp.com/add/1/-2 will call the negative function.

Ni.addRoute(function(path) {
    var args = path.split('/'),
    firstNum = parseInt(args[2]),
    secondNum = parseInt(args[3]),
    result = firstNum + secondNum;
    if (args[1] !== 'add')
        return false; // this leaves the path untouched and prevents this function from sucking in all other requests as well
    return result > 0 ? '/Number/positive' : '/Number/negative';
});

You can limit the allowed HTTP methods by using custom routes. This will internally redirect myapp.com/comment to use your Comments controler and its new function - but only if the used HTTP Method is POST or PUT:

Ni.addRoute('/comment', '/Comments/new', ['POST', 'PUT']);

And this will redirect all GET requests to myapp.com/comment to use your Comments controller's index function.

Ni.addRoute('/comment', '/Comments/', 'GET');

This way you can disallow methods for some routes as well:

Ni.addRoute('/Comments/update', '/Home/method_not_allowed', 'GET');

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.controller('calculator')
  • Ni.view('calculator')
  • Ni.model('calculator')

A really well-commented example is in the source code in the /example folder, check it out!

(Note that you'll need the following to run the example in the /example folder: Connect, Quip and Mu. You can get them easily using npm.)

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, next) {

        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, next, a, b) {

        if (a && b) {
            a = parseInt(a);
            b = parseInt(b);

            var template = Ni.view('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 crackin'!

  1. Get Ni by downloading the source code, or git cloneing the repo.
  2. Install Ni as your would any other Node module, by copying lib/ni.js to your ~/.node_libraries folder.
  3. Copy the /example directory and modify it to set up your project.
  4. Run node app.js in the copied directory to start the server.

(We'll add Ni to npm soon, don't trip.)

Contributors

A big thank you to you guys who helped (and are helping) make Ni awesome:

Moritz Peters

More Repositories

1

sidestep

A Mac OS X application that automatically secures your Internet connection in unprotected wireless networks through SSH tunneling.
Objective-C
282
star
2

deadmouse

Click on links without the mouse; just start typing. A Google Chrome extension.
CoffeeScript
63
star
3

linguist

A language-learning AI based on NuPIC
Python
33
star
4

jquery.fingers

touch gesture detection with jQuery
CoffeeScript
11
star
5

unity-api

Plugin to add a programmatic interface to a Unity project
Python
9
star
6

panels

Bookmarklets to pull up or start the Hacker News or Reddit discussions for any web page
Ruby
7
star
7

rails-bootstrap

Create a boilerplate Rails project, furnished with all the gems, libraries and dependencies you need to get started immediately.
Ruby
6
star
8

anomalous-driving

A Unity demo of anomaly detection using NuPIC
C#
6
star
9

ABBC

CS161 Social Networking Project, Group ABBC
Java
5
star
10

pilot

A motor control AI based on NuPIC
Python
5
star
11

sandbox

This is just an area for me to play with little ideas. Don't mind me.
Python
4
star
12

mockies

A collection of common HTML / CSS snippets and elements for rapid prototyping and development
3
star
13

node-readability-deprecated

Deprecated in favor of arrix's node-readability (https://github.com/arrix/node-readability)
JavaScript
3
star
14

nounce

C
3
star
15

timebend

Today, do everything and more. A time-management app.
HTML
3
star
16

nupic.hangouts

An example of using NuPIC to detect anomalies in Google Hangouts conversations.
Python
3
star
17

dotfiles.old

My configuration files
Vim Script
2
star
18

headlyne

A more natural way to read your feeds.
JavaScript
2
star
19

eX

[Currently on hold] A simpler syntax that compiles into LaTeX for cleaner and speedier document creation (think of it as Markdown for LaTeX)
2
star
20

chetansurpur

my personal website and blog
Ruby
2
star
21

babble

Watch-as-I-type IM. An experiment using Meteor.
JavaScript
2
star
22

genesys

Evolving Programs to Solve Problems [WIP]
Python
2
star
23

highkeygames.presskit

Presskit for HIGHKEY Games
HTML
1
star
24

nupic.js

NuPIC port in JavaScript
JavaScript
1
star
25

learning-webgl

Experiments with WebGL / Three.js
JavaScript
1
star
26

twss

TWSS classifier trained with a Naive Bayes unigram model
OpenEdge ABL
1
star