NodeJS

Node.js is a platform built on Chrome's Javascript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

http://www.nodejs.org/

SlackBuild Slackware64 14.0

Package 64 bit: node-0.10.12-x86_64-1_SBo.tgz

Sample app

main.js

   1 var assert = require('assert'); // TDD
   2 var fs = require('fs'); // file system
   3 var util = require('util');
   4 //--------------------
   5 var Dummy = require('./dummy.js');
   6 var Local = require('./local.js');
   7 var conf=require('./config.js') // Configuration
   8 
   9 console.log( conf.configX['foo'] );
  10 console.log( conf.configX['bar'] );
  11 
  12 var d1 = new Dummy(12345);
  13 var d2 = new Dummy(67890);
  14 var d3 = new Dummy(112233);
  15 
  16 assert.equal(d1.methodA.call(d2),67890,'');
  17 assert.equal(d1.methodA.call(d3),112233,'');
  18 assert.equal(d3.methodA.call(d1),12345,'');
  19 
  20 var l1 = new Local('local1',10,11);
  21 var l2 = new Local('local2',20,21);
  22 var l3 = new Local('local3',30,31);
  23 
  24 assert.equal(l1.getPoint().getX(),10,'');
  25 assert.equal(l2.getPoint().getX(),20,'');
  26 assert.equal(l3.getPoint().getX(),30,'');
  27 
  28 // async readdir
  29 fs.readdir('/tmp', cbReaddir);
  30 
  31 function cbReaddir(err,files){
  32     for(var i=0; i< files.length;i++){
  33         console.log(files[i]);
  34     }
  35 }
  36 
  37 //async append file
  38 fs.appendFile('/tmp/test.txt', 'data to append\r\n', cbAppendFile);
  39 
  40 function cbAppendFile(err) {
  41     if (!err) {
  42         console.log('The "data to append" was appended to file!');
  43     }
  44 }
  45 
  46 function hexcounter(value,callback){
  47     console.log('HC:',value);
  48     if(value<15){
  49       setImmediate(function(){callback(value+1,callback);});
  50     }
  51     else{
  52       setImmediate(function(){callback(0,callback);});
  53     }
  54 }
  55 
  56 function octalcounter(){
  57     var timeout=500;
  58     if(arguments.length===1){
  59         console.log('OC:',arguments[0]);
  60         setTimeout(octalcounter,timeout,1,2)
  61     }
  62 
  63     if(arguments.length===2){
  64         var a0=Number(arguments[0]);
  65         var a1=Number(arguments[1]);
  66         console.log(util.format('%s %d %d','OC:',a0,a1));
  67         setTimeout(octalcounter,timeout,a0+1,a1+1);
  68     }
  69 }
  70 
  71 hexcounter(2,hexcounter);
  72 octalcounter(12300);
  73 
  74 process.on('SIGHUP', onSIGHUP);
  75 function onSIGHUP() {
  76   console.log('SIGHUP received',arguments.length);
  77 }
  78 
  79 process.on('SIGTERM', onSIGTERM);
  80 function onSIGTERM() {
  81   console.log('SIGTERM received',arguments.length);
  82 }

config.js

   1 exports.configX=
   2     {
   3         "foo":"fooValue",
   4         "bar":123
   5     }

dummy.js

   1 function Dummy(value1) {
   2     var self = this;
   3     self._value1 = value1;
   4 }
   5 
   6 Dummy.prototype.methodA = function() {
   7     var self = this;
   8     return self._value1;
   9 };
  10 
  11 module.exports = Dummy;

point.js

   1 function Point(x,y){
   2     this.x=x;
   3     this.y=y;
   4 }
   5 
   6 Point.prototype.getX=function(){
   7     return this.x;
   8 };
   9 
  10 Point.prototype.getY=function(){
  11     return this.y;
  12 };
  13 
  14 module.exports = Point;

local.js

   1 var Point = require('./point.js');
   2 
   3 function Local(name,x,y){
   4     this.name = name;
   5     this.point = new Point(x,y);
   6 }
   7 
   8 Local.prototype.getName=function(){
   9     return this.name;
  10 };
  11 
  12 Local.prototype.getPoint=function(){
  13     return this.point;
  14 };
  15 
  16 module.exports = Local;

node-gyp

node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js.

https://github.com/TooTallNate/node-gyp

Install with npm

Hello world gyp

Based on https://github.com/joyent/node/tree/master/test/addons/hello-world

Based on https://github.com/rvagg/node-addon-examples

{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "hello.cc" ]
    }
  ]
}

   1 #include <node.h>
   2 #include <v8.h>
   3 
   4 using namespace v8;
   5 
   6 Handle<Value> Method(const Arguments& args) {
   7   HandleScope scope;
   8   return scope.Close(String::New("world"));
   9 }
  10 
  11 void init(Handle<Object> exports) {
  12   exports->Set(String::NewSymbol("hello"),
  13       FunctionTemplate::New(Method)->GetFunction());
  14 }
  15 
  16 NODE_MODULE(hello, init)

   1 var addon = require('./build/Release/hello');
   2 
   3 console.log(addon.hello()); // 'world'
   4 

Javascript/NodeJS (last edited 2014-03-12 23:14:48 by 188)