node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

supernova

Introduction

Supernova is a V8 engine based framework to work with classes, inheritance and encapsulation of modules for the web. Every instance of a supernova class can be rendered with a view-enigne (default: jade) and has its own template. The built in router and webserver stack gives you a full stack for writing webapplications with supernova!

Example App

app.js

var nova = require('supernova');
 
var Person = nova.create({
    constructor: function(name, age) {
        this.name = name;
        this.age = age > 0 ? age : 'n.A.';
    },
    template: './person.jade'
});
 
var foo = new Person('bar', 10);
var baz = new Person('foo', 39);
 
var router = nova.router();
var server = nova.server();
 
router.get('/', function(req, res, d) {
    var markup = nova.render({
        users: [foo, baz]
    });
 
    res.write(markup);
});
 
server.attach(router).listen(1996);

Example page view

page.jade

doctype 5
    html(lang=en)
        head
            title= Users
        body
            block users
 
 

Example module view

person.jade

extends page
 
block users
    if users && users.length > 0
        ul
        each user in users
            li= #{user.name} (Age #{user.age})
    else
        p No users avaible!

Class Basics

Creation

With supernova you can easily create classes with properties, methods and constructors. They're provided with getters, setters, special methods and even an event-system, which you can disable by passing a second boolean parameter true to the method. However, you can also inherit multiple times in a tree below, which is shown in the chapter Advanced Inheritance, but let's start with the basic example;

1. Include

var nova = require('supernova');

2. Create

var MyClass = nova.create();

Well, you now got a class, which has no content but the basic methods of the supernova.Base class. So, let's change the class creation a little bit and add some properties and methods;

var MyClass = nova.create({
    // default name 
    name: '<anonym>',
 
    // class constructor 
    constructor: function(name) {
        this.name = name;
    },
 
    hellow: function() {
        console.log('Hellow, I\'m %s!', this.name);
    }
});

So, we've created a basic class with a name property in it, and a method to greet somebody - called hellow. Let's see it in action!

3. Instanciate

var test = new MyClass();
test.hellow(); // > 'Hellow, I'm <anonym>!' 

Now, we can instanciate the class with a value, any name or number or whatever you like to insert. Let's try it with 'Foo';

var test2 = new MyClass('Foo');
test.hellow(); // // > 'Hellow, I'm Foo!' 

And that's the magic!

Advanced Inheritance

You basically can inherit classes from another class created with supernova. It will be stored in a prototype chain with references to the super class. Everything is implemented correctly, so that even the instanceof method will return true if you test it on the parent! Here's an example below;

var MySuper = nova.create({
    name: '<anonym>',
    setName: function(name) {
        this.name = name;
    }
});
 
var MyChild = MySuper.create({
    construct: function(name) {
        this.setName(name);
        console.log('Hellow, I\'m %s!', this.name);
    }
});
 
var You = new MyChild('Max');
// > 'Hellow, I'm Max!' 

Events

Basics

Every class created with supernova will automatically inherit properties and methods from the EventEmitter, a class from nodes built-in events module. So, every instance can use the classic event handling system, like in the example below.

var nova = require('supernova');
 
var Test = nova.create({
    constructor: function(num) {
        this.data = num;
    },
    calculate: function(mult) {
        this.emit('calculate:after', this.data * (mult || 1));
    }
});
 
var app = new Test(10);
 
app.on('calculate:after', function(res) {
    console.log('Calculated number %d!', res);
});
 
app.calculate(2); // > 'Calculated number 20!' 

View Basics

The view engine in this framework is jade but if you'd like to, you can write your own view engine. For this path, take a look at custom view-engine.

Engine

The view engine or renderer is exported under .render and takes two arguments; an instance and an object - which contains additional options. This method returns the compiled jade HTML as string.

var nova = require('supernova');
 
var person = nova.create({
    name: '<anonym>',
    template: './path/to/file/person.jade'
});
 
var bill = new person();
bill.name = 'Bill';
 
var html = nova.render(bill);

And now it's time to write the jade HTML template, this is a simple template and if you want to learn more about jade, visit their Language Reference documentation.

!!!5
  html
    head
      title Supernova App
      include styles
    body
      h1 Hey there, I'm #{bill.name}!  

Custom Engine

tbd.

Server basics

Create

Supernova has an integrated server which allows you to build webapplications rapidly and without requiring other node modules. It's a simple HTTP server with a lot of customization, but let us have a look at that;

var router = nova.router();
var server = nova.server();
 
router.get('/', function(req, res, d) {
    res.write('Hellow World');
});
 
server.attach(router).listen(2000);

Run

You need to call the .listen method on the server to get him up and running. It will take the port you've set in the options in the server or alternatively you can also define the port for the server which he will listen to via the listen function (arg#1). Default port is 7007!

server.listen(); // http://localhost:7000 

Custom server

tbd.

Router

Supernova contains a basic router for creating RESTful webapplications with ease. If you don't want to use the built-in router but want to write your app with express or another webapplication framework, you can use them and make the rest with supernova. So you stay fully flexibel with it and you can change it anytime you'd like to. Here is a short example of how to use the router;

var server = nova.server();
var router = nova.router();
 
// on HTTP GET requests 
router.get('/user/:name', function(req, res, data) {
    var html = nova.render(new Usersite({
        name: data[1] // will be :name 
    }));
 
    res.write(html); // output 
});
 
// on HTTP PUT requests 
router.put('/user/:name', function(req, res, data) {
    var name = data[0];
    // do whatever you want with the name ... 
});
 
// connect your router and take-off 
server.attach(router).listen(1232);

Framework Information

Author

This framework was created by Jan Biasi in 2015 and is licensed under the MIT License.

Versions

Version Description Updatable?
1.0.17 Readme file updates and fixes in example code yes
1.0.16 Readme file updates and fixes in example code yes
1.0.15 Minor bugfixes in server stack yes
1.0.14 Adding a basic router inspired by expressjs no
1.0.13 Bugfixes in server, updated architecture and prepare for router yes
1.0.12 Adding example application yes
1.0.11 Updated documentation for 10.0.01+ yes
1.0.10 Updated readme and update npm package yes
1.0.09 Server-stack bugfixes yes
1.0.08 Server-stack bugfixes yes
1.0.07 Server-stack bugfixes yes
1.0.06 Add the server-stack for webapps yes
1.0.05 Templates can now be defined in classes yes
1.0.04 Jade can work with files now yes
1.0.03 Partial view support for Jade yes
1.0.02 Including dependencies; jade, chalk, pathregexp & co. yes
1.0.01 Repair bugs in subclass creation and add prototype chaining yes
1.0.00 Basic version without view engine and server yes
0.0.x-beta Local work -
0.0.x-alpha Local work -