MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 19 as of 2015-03-12 21:27:30
  • Javascript
  • NodeJS

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/

http://blog.4psa.com/the-callback-syndrome-in-node-js/

SlackBuild Slackware64 14.0

  • su
  • cd /tmp
  • wget http://slackbuilds.org/slackbuilds/14.0/network/node.tar.gz

  • tar xvzf node.tar.gz
  • cd node
  • wget http://nodejs.org/dist/v0.10.12/node-v0.10.12.tar.gz

  • ./node.SlackBuild

  • installpkg /tmp/node-0.10.12-x86_64-1_SBo.tgz

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

  • su
  • npm install -g node-gyp
  • node-gyp

Hello world gyp

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

  • cd /tmp/
  • nano test.js
  • nano binding.cc
  • nano binding.gyp
  • node-gyp configure #The configure step looks for the binding.gyp file in the current directory
  • node-gyp build # gave an error building !

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

  • nano binding.gyp

{
  "targets": [
    {
      "target_name": "hello",
      "sources": [ "hello.cc" ]
    }
  ]
}
  • nano 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)
  • nano hello.js

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

Build script

   1 #!/bin/sh
   2 GYP=/usr/local/lib/node_modules/npm/bin/node-gyp-bin/node-gyp
   3 $GYP clean
   4 $GYP configure
   5 $GYP build

Read text file example

   1 var fs = require('fs'); // file system
   2 
   3 function cbReadFile(err, data) {
   4         if (err) {
   5                 throw err;
   6         }
   7         var ds = data.toString(); // data is of type Buffer
   8         var buffer = '';
   9         for ( var idx = 0; idx < ds.length; idx++) {
  10                 if (ds[idx] != '\n') {
  11                         buffer = buffer + ds[idx];
  12                 } else {
  13                         console.log(buffer);
  14                         buffer = '';
  15                 }
  16         }
  17 
  18 }
  19 
  20 fs.readFile('/etc/passwd', cbReadFile);

Context with this and bind

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

   1 /*Constructor function*/
   2 function Counter(){     
   3     this.counter=0;
   4 }
   5 
   6 Counter.prototype.run=function(){
   7     console.log('counter:',this.counter);
   8     this.counter++;
   9     //recall run
  10     setTimeout(this.run.bind(this),1000);
  11 };
  12 
  13 var c = new Counter();
  14 setTimeout( c.run.bind(c) ,1000); // apply context of c to this
  15 
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01