Node.JS 4.Basics

 

Encoding:

Node.JS 支持 UTF-8("utf8")ASCII("ascii") 以及 Binary("binary") 编码,相对来说,ASCII Binary 会快一些,UTF-8 会慢许多并且应该尽量避免使用它。

 

Globals

Arguments:


var sys = require('sys'),
    some_argument = process.argv[2];

// argument example

if(!some_argument){
    return sys.puts('Usage: node ' + __filename.replace(__dirname + '/', '') + 'some_argument');
}

 
//require example
sys.puts('Default require paths: ' + require.paths);

sys.puts('Adding current directory to require.paths');

require.paths.unshift(__dirname);

sys.puts('Modfied require.paths: ' + require.paths);
 

 


 

接着运行

 

node global.js test_arg

会输出如下信息:

Adding current directory to require.paths

Modified require.paths: /home/sinsay/projects/nodejs_example/4.bacics/global.js,/home/sinsay/.node_modules,/home/sinsay/.node_libraries,/usr/local/lib/node

sinsay@ubuntu:~/projects/nodejs_example/4.bacics$ node global.js

 

process.argv 保存了命令行的参数。在当前示例中, argv[0] node, argv[1] global.js,

argv[2] 即为参数 some_argument


if(!some_argument){
    return sys.puts('Usage: node ' + __filename.replace(__dirname + '/', '') + 'some_argument');
}

 

 

如果执行的时候没有带上参数,当前脚本会输出该脚本的正确使用方法。而在上面的代码中,__filename是一个全局的变量,他表示当前执行的文件路径,

并且他替换了 __filename 里的路径(__dirname 也是全局变量,表示当前路径),只保留了文件名。

 

require

require.paths

提供了 require 在查找 package 或其他东西时的路径,可以使用 unshift 来为查找路径添加你的目录,就像:

require.paths.unshift(__dirname);

 

Process

process 对象为我们提供了当前进程的信息,如进程 ID运行的平台,内存占用等等

你也可以用在退出脚本的时候提供退出的状态码,e.g:

 

process.exit(0);

 

而在默认情况下他的退出状态码就是为 0 的,下面看示例。

 

var sys = require('sys');

//dis play all command line arguments
sys.puts('Provided arguments : ');
for(var key in process.args){
    sys.puts(key + ' : ' + process.argv[key]);
}

//process details (PID, platform, memory usage)
sys.puts('\nPID: ' + process.pid);
sys.puts('Platform: ' + process.platform);
sys.puts('Memory usage: ' + process.memoryUsage().rss);

//display user environment
sys.puts('\nUser Environment : ');
for(var key in process.env){
    sys.puts(key + ' : ' + process.env[key]);
}

//process exit code - default success code 0
process.exit(0);

 

System module

system 模块提供了许多方式让我们向控制台输出信息

 

sys.puts() //输出信息并换行

sys.print() //输出信息但不换行

sys.debug('some debug output');

//输出信息,并加上DEBUG 信息,e.g:

//DEBUG: some debug output

system.log('some log output');
//输出信息,并加上当前的日期跟时间,e.g:
//
20 Mar 23:17:15 - some log output

sys.inspect(process.memoryUsage())
//按对象自身的呈现形式输出信息,e.g:
//
{ rss: 5263360
//
, vsize: 41353216
//
, heapTotal: 2176512
//
, heapUsed: 963872
//
}

 

以下是示例脚本:

var sys = require('sys');

//sys output examples
sys.puts('Output with trailing newline');
sys.print('Output without ');
sys.print('new line');
sys.puts('\nAdd new line to beginning and extra one at the end.\n');
sys.debug('Some debug output');
sys.log('Some log output');

// simple sys.inspect example
var process_memory = process.memoryUsage();
sys.puts('\nprocess.memoryUsage(): ');
sys.puts(sys.inspect(process_memory));

 

 

Timers

 

你可以在node 的程序中使用 JavaScript 定时器,比如 setTimeout(), clearTimeout(), setInterval(), clearInterval()。示例:


var sys = require('sys');

// simple timeout exapmle
var start_time = new Date();
sys.puts('String 2 second timer');

setTimeout(function(){
    var end_time = new Date();
    var difference = end_time.getTime() - start_time.getTime();
    sys.puts('Stopped timer after : ' +
Math.round(difference / 1000) + 'seconds');

    cleartimeout_example();
}, 2000);


function cleartimeout_example(){
    var start_time = new Date();
    sys.puts('\nStarting 30 second timer and stopping it immediately without triggering callback');

    var timeout = setTimeout(function(){
        var end_time = new Date();
        var difference = end_time.getTime() - start_time.getTime();
        sys.puts('Stoped timer after ' + Math.round(difference / 1000) + ' seconds');
    }, 3000);
    clearTimeout(timeout);
    interval_example();
}


function interval_example(){
    var start_time = new Date();
    sys.puts('\nString 2 second interval, stopped after 5th tick');

    var count = 1;
    var interval = setInterval(function(){
        if(count == 5) clearInterval(this);
        var end_time = new Date();
        var diffrence = end_time.getTime() -
start_time.getTime();
        sys.puts('Tick no.' + count + 'after ' + Math.round(diffrence / 1000) + ' second');
        count ++;
    }, 2000);
}

 

posted on 2011-09-27 12:24  兴说:  阅读(970)  评论(0编辑  收藏  举报