Size: 2007
Comment:
|
Size: 5125
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 18: | Line 18: |
== Sample app == main.js {{{#!highlight javascript var assert = require('assert'); // TDD var fs = require('fs'); // file system var util = require('util'); //-------------------- var Dummy = require('./dummy.js'); var Local = require('./local.js'); var conf=require('./config.js') // Configuration console.log( conf.configX['foo'] ); console.log( conf.configX['bar'] ); var d1 = new Dummy(12345); var d2 = new Dummy(67890); var d3 = new Dummy(112233); assert.equal(d1.methodA.call(d2),67890,''); assert.equal(d1.methodA.call(d3),112233,''); assert.equal(d3.methodA.call(d1),12345,''); var l1 = new Local('local1',10,11); var l2 = new Local('local2',20,21); var l3 = new Local('local3',30,31); assert.equal(l1.getPoint().getX(),10,''); assert.equal(l2.getPoint().getX(),20,''); assert.equal(l3.getPoint().getX(),30,''); // async readdir fs.readdir('/tmp', cbReaddir); function cbReaddir(err,files){ for(var i=0; i< files.length;i++){ console.log(files[i]); } } //async append file fs.appendFile('/tmp/test.txt', 'data to append\r\n', cbAppendFile); function cbAppendFile(err) { if (!err) { console.log('The "data to append" was appended to file!'); } } function hexcounter(value,callback){ var timeout=500; console.log('HC:',value); if(value<15){ setTimeout(function(){callback(value+1,callback);},timeout); } else{ setTimeout(function(){callback(0,callback);},timeout); } } function octalcounter(){ var timeout=500; if(arguments.length===1){ console.log('OC:',arguments[0]); setTimeout(octalcounter,timeout,1,2) } if(arguments.length===2){ var a0=Number(arguments[0]); var a1=Number(arguments[1]); console.log(util.format('%s %d %d','OC:',a0,a1)); setTimeout(octalcounter,timeout,a0+1,a1+1); } } hexcounter(2,hexcounter); octalcounter(12300); process.on('SIGHUP', onSIGHUP); function onSIGHUP() { console.log('SIGHUP received',arguments.length); } process.on('SIGTERM', onSIGTERM); function onSIGTERM() { console.log('SIGTERM received',arguments.length); } }}} config.js {{{#!highlight javascript exports.configX= { "foo":"fooValue", "bar":123 } }}} dummy.js {{{#!highlight javascript function Dummy(value1) { var self = this; self._value1 = value1; } Dummy.prototype.methodA = function() { var self = this; return self._value1; }; module.exports = Dummy; }}} point.js {{{#!highlight javascript function Point(x,y){ this.x=x; this.y=y; } Point.prototype.getX=function(){ return this.x; }; Point.prototype.getY=function(){ return this.y; }; module.exports = Point; }}} local.js {{{#!highlight javascript var Point = require('./point.js'); function Local(name,x,y){ this.name = name; this.point = new Point(x,y); } Local.prototype.getName=function(){ return this.name; }; Local.prototype.getPoint=function(){ return this.point; }; module.exports = Local; }}} |
|
Line 24: | Line 175: |
su npm install -g node-gyp node-gyp |
* su * npm install -g node-gyp * node-gyp |
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.
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
./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 var timeout=500;
48 console.log('HC:',value);
49 if(value<15){
50 setTimeout(function(){callback(value+1,callback);},timeout);
51 }
52 else{
53 setTimeout(function(){callback(0,callback);},timeout);
54 }
55 }
56
57 function octalcounter(){
58 var timeout=500;
59 if(arguments.length===1){
60 console.log('OC:',arguments[0]);
61 setTimeout(octalcounter,timeout,1,2)
62 }
63
64 if(arguments.length===2){
65 var a0=Number(arguments[0]);
66 var a1=Number(arguments[1]);
67 console.log(util.format('%s %d %d','OC:',a0,a1));
68 setTimeout(octalcounter,timeout,a0+1,a1+1);
69 }
70 }
71
72 hexcounter(2,hexcounter);
73 octalcounter(12300);
74
75 process.on('SIGHUP', onSIGHUP);
76 function onSIGHUP() {
77 console.log('SIGHUP received',arguments.length);
78 }
79
80 process.on('SIGTERM', onSIGTERM);
81 function onSIGTERM() {
82 console.log('SIGTERM received',arguments.length);
83 }
config.js
dummy.js
point.js
local.js
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
- node hello.js