文章目录
  1. 1. Node.js开发的常用模块总结
    1. 1.1. console
    2. 1.2. URL与QueryString
    3. 1.3. Path与File System
    4. 1.4. Stream
    5. 1.5. 定时器
    6. 1.6. child process
      1. 1.6.1. 创建进程
      2. 1.6.2. 选项说明
      3. 1.6.3. ChildProcess对象
    7. 1.7. global

Node.js开发的常用模块总结

因为玩hexo的静态blog的原因,为了满足自己的需求,学了学node.js,自己动手写了几个hexo的插件。开发告一段落,估计过两个星期就把node.js忘了,跑来总结下。

console

console模块是Node提供的核心模块,提供基本的输出功能。虽然node.js采用的js语法,但是你千万不要在node.js使用alert进行调试,那是浏览器带的全局方法,会报错的。

1
2
3
4
5
6
7
8
9
console.log([format],...) ##向控制台输出日志,类似的API还有info() err() warn(),
##格式有%s,%d,%j(json格式)
console.info()
console.warn()
console.error() ##与console.log()差不多
e.g.
console.log('%s,%d,%j','hello world',0,{name:'coney',Sexy:'Male',age:18,do:['hexo','node.js','themes']});
##输出:
hello world,0,{"name":"coney","Sexy":"Male","age":18,"do":["hexo","node.js","themes"]}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
console.dir(object) ##利用util.inspect()输出对象的分析
e.g.
var Hexo = function(root,theme,version)
{
this.root=root;
this.themes=theme;
this.version=version;
};
var myBlog = new Person('Users/workspace','coney',1.0);
console.dir(myBlog);
console.dir(Hexo);
## 输出
{root: 'Users/workspace', theme:'coney', version: 1.0}
[Function]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
console.time(tag)
console.timeEnd(tag) ##配合time() 生成一个测时器,并输出其间隔的时间
##time和timeEnd中不要输入中文,
##time和timeEnd中输入的参数要一致
e.g.
var tag = 'the time-consuming of sum operate';
console.time(tag);
var sum=0;
for(var i=0;i<10000;i++)
{
sum+=i;
}
console.log('sum=%d',sum);
console.timeEnd(tag);
##输出:
sum=49995000
the time-consuming of sum operate: 9ms
1
2
3
4
5
console.assert(expr,msg) ##若expr为假,则输出msg
e.g.
console.assert(1==2,'above is wrong');
##输出
AssertionError,above is wrong
1
console.trace(tag) ##输出当前的栈信息

URL与QueryString

这两个模块是Node提供的核心模块,用于处理URL及URL中的查询串。通常情况下,不需要直接处理querystring,因为url模块的支持。

1
2
3
4
5
6
7
8
9
url.parse(url,qs,sl)
##将url解析为一个对象,其中包含host,hostname,port等字段,
##若qs为true,则调用querystring解析查询串
url.format(obj)
##将obj生成为一个url字符串
querystring.stringify(obj,'sep','eq')
##将对象obj生成查询串
querystring.parse(str)
##解析查询串为对象

Path与File System

Path模块则提供了对路径名称的一些便捷操作,其将输入串作为路径栈来处理,与物理文件系统无关。

1
2
3
4
5
6
7
path.normalize('path') ##处理path中的' . '与'..'
path.join(a,b,..z) ##处理为a/b/.../z,并对结果执行normalize
path.resolve(a,b,...z) ##对每个元素执行cd item,返回最终结果
path.relative(a,b) ##返回b对于a的切换方式,可包含'.'与'..'
path.dirname(path) ##path.basename(path,'mask') 返回路径的目录部分与文件名部分
path.extname(path) ##返回路径的扩展名部分
path.seq ##OS特定的路径分隔符 "\\" 或 "/"

File System提供了对文件与目录的创建、删除、权限设置,并以及文件的打开、读写与关闭。文件API提供两套接口,分别支持同步与异步调用。区别是,同步情况异常会立即抛出,需要用try-catch捕获,而在异步情况下,异常会传递给回调函数处理。值得注意的是异步情况下,不同的事件的完成顺序是未决的,因而对于有顺序的操作,最好在函数的事件链中去完成。由于同步操作与POSIX同名调用类似,因此这里只介绍异步接口。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
fs.rename(oldPath, newPath, [callback])
##修改名字,回调函数为function(exception) {}
fs.truncate(fd, len, [callback]) ##截断文件
fs.chown(path, uid, gid, [callback])
fs.fchown(fd, uid, gid, [callback])
##修改文件所属
fs.chmod(path, mode, [callback])
fs.fchmod(fd, mode, [callback])
##修改文件权限
fs.stat(path, [callback])
fs.lstat(path, [callback])
fs.fstat(fd, [callback])
##返回文件stat,回调为function(err,state) {}
fs.link(srcpath, dstpath, [callback])
fs.symlink(srcpath, dstpath, [type], [callback])
##创建链接与符号链接
fs.readlink(path, [callback])
##读取link的内容
fs.unlink(path, [callback])
##取消link
fs.rmdir(path, [callback])
fs.mkdir(path, [mode], [callback])
##目录创建与删除
fs.readdir(path, [callback])
##读取目录内容 回调为function(err,files) {}
fs.open(path, flags, [mode], [callback])
##回调为function(err,fd) {}
fs.write(fd, buffer, offset, length, position, [callback])
##回调为function(err,len,buf) ;
fs.read(fd, buffer, offset, length, position, [callback])
##回调为function(err,len,buf) ;
fs.close(fd, [callback])
fs.utimes(path, atime, mtime, [callback])
fs.futimes(fd, atime, mtime, [callback])
##修改文件的访问时间与修改时间
fs.fsync(fd, [callback])
fs.readFile(filename, [encoding], [callback])
##高层接口,回调为function(err,data)
fs.writeFile(filename, data, [encoding], [callback])
##高层接口,回调为function(err)
fs.appendFile(filename, data, encoding='utf8', [callback])
fs.createReadStream(path,[opt])
fs.createWriteStream(path,[opt]) 从路径创建输入与输出流

Stream

Stream是Node实现的一种面向流处理的接口,支持文件流、HTTP数据流、标准IO流等进行抽象操作。
Stream分为readStream与writeStream两种类型
read stream 定义了下列事件 data,end,error,close 用于在底层处理流的过程中回调用户的事件监听器。

1
2
3
4
5
setEncoding('charset') ##设置流的字符编辑,默认为utf-8
pause() resume()
##暂停与恢复流处理,需要注意的是有些底层处理并不能立即生效, ##在pause一段时间内仍会抛出data事件
destroy() ##在流处理结束后,释放流使得底层可以关闭文件
pipe(dstream,[opt]) ##将输入流复制到目标输出流

writable Stream定义了下列事件:

drain,error,close,pipe 用于底层在处理流的过程中回调用户的事件监听器

1
2
3
4
5
write(string,[charset])
write(buff) 写入输出流
end()
end(string,encoding) end(buf) 定稿输出流并写入EOF
destory()/destroySoon() 关闭底层资源,后者后将写入缓存区写空后释放。

定时器

定时器操作被导出到全局命名空间中,不需要单独包含

1
2
3
4
setTimeout(callback, delay, [arg...]) 设置超时执行
clearTimeout(timeoutId)
setInterval(callback, delay, [arg...]) 设置间隔执行
clearInterval(intervalId)

child process

此模块提供了操作子进程以及与进程间通信的基本功能。这个模块提供执行子进程的四种方式,并返回子进程的抽象,ChildProcess类型的实例。子进程类型其实是一个事件发生器,其上定义了子进程生命周期中的各种事件,如exit, close, message,disconnect事件。实例可以取到对应进程的相关属性如标准输入、输出、错误、进程ID,此外还提供了对进程发送消息的接口send,kill()和disconnect()。

创建进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spawn(cmd, [args,...], [opt])
##基于cmd命令执行一个子进程,命令的参数为args,若无则为空
##数据。option={cwd, stdio, customFds,
##env, detatched}
exec(cmd,[opt],callback )
##基于cmd命令执行子进程,命令参数为空格分隔。回调的原型为
##function(err,stdout, stderr) {}。
##option={cwd, stdio, customFds, env,encoding,
##timeout,maxBuffer,killSignal}
execfile(file , [args,...], [opt],callback)
##对于非shell命令的程序,可以通过execfile来执行,
##回调函数原型为function(err,stdout, stderr) {}。
##option={cwd, stdio, customFds, env,encoding,
##timeout,maxBuffer,killSignal}
fork(module, [args,...],[opt])
##在子进程中执行一个node模块。
##option={cwn,env,encoding}.

选项说明

  1. cwd:当前工作区

  2. env: 进程执行环境

  3. stdio: 一个数组,数组每个位置对应子进程的一个文件fd,每个位置的值可能为pipe, ipc,ignore,stream obj, null, 正数。pipe表示创建一个父子间的管道,父进程可以通过child_process.stdio[fd]来获取写入端。IPC,创建一个IPC信道。可以使用send()来向此文件fd进行消息的传递,并在接受方引发on(‘message’)事件。 ignore:忽略IO,重定向到/dev/null。

  4. Stream obj:在进程间共享一个re/wt 流对象,以操作子进程的TTY,FILE,PIPE,SOCKET等。正整数:其值表示在父进程中打开的文件fd,共享给子进程。null,使用默认值。0,1,2为pipe,>3的为ignore。为了方便书写, node定义了三个数组,’ignore’,’pipe’,’inherit’,分别表示对于0-2的文件进行相应的操作
    例如,如果想让子进程直接使用父进程的输出,可以require(‘child_process’).spawn(‘ls’,[ ],{stdio:‘inherit’});

  5. detached:让子进程成为新进程组的首进程,从而在父进程退出后仍可继续执行。默认情况下父进程会等子detached进程的结束,可以通过child.unref()来避免这种等待。尽管如此,若希望子进程正常执行,仍需要将子进程与父进程的控制终端相剥离。

  6. timeout:若>0,则在执行超时后由选项指定的信号终止。

  7. killSignal:指定期望接收的终止信号

  8. maxBuffer:指定标准输出和错误的最大数据量,超出后子进程被kill

ChildProcess对象

本类是子进程的抽象对象,支持以下操作和属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
kill(signal)
##使用信号终止进程
send(msg, [sendHandler])
##这里需要注意的是,在父进程中调用send()的事件会被子进程注册的事件处理
##函数处理,而在子进程中给自己send()会触发在父进程中注册的事件处理函数。
disconnect()
stdin,stdout,stderr, Readable Stream或Writable Stream
pid:
##子进程的PID
##子进程在执行过程中会触发以下事件:
exit:
##进程终止时触发。回调function(code,signal) ,若进程正常结束,则code为执行进程的
##退出码,否则为null。若进程由信号终止,则signal为收到的信号。
close
##进程的 stdio流结合时触发
disconnect:
##调用dieconnect()后触发,调用后不可以再向子进程发送消息,可以检查
##child.connected属性
message:
##在调用send()后触发,function(msg,sendHandler){}
##sH可能为socket对象,或server对象。

global

全局空间可以访问console,process,buffer,timer模块,不需要require
console与timer在上面已有说明,process提供了node执行进程的运行环境、参数、进程的gid,uid,运行时间等等,具体可以查找process模块

1
2
__filename ##当前执行脚本的文件名
__dirname ##当前执行脚本的所在目录
文章目录
  1. 1. Node.js开发的常用模块总结
    1. 1.1. console
    2. 1.2. URL与QueryString
    3. 1.3. Path与File System
    4. 1.4. Stream
    5. 1.5. 定时器
    6. 1.6. child process
      1. 1.6.1. 创建进程
      2. 1.6.2. 选项说明
      3. 1.6.3. ChildProcess对象
    7. 1.7. global