node.js-v0.8API解读(2)- api的修改部分

0.8版本修改了不少api,以下是一些常用的api修改,大家注意哦:

以下是主要常用的api变更

1、path

path.{exists,existsSync} was moved to fs.{exists,existsSync}

很早就觉得path.exists应该归纳到fs模块中

2、cluster

cluster.fork() no longer return a child_process.fork() object use cluster.fork().process to get the object.

返回子进程的process对象需要使用:cluster.fork().process

the ‘death’ event on the cluster object is renamed to ‘exit’.
cluster的death事件改名为exit

the kill() method is renamed to destroy().
cluster的kill方法改名为destory

the CLUSTER_WORKER_ID env is now called CLUSTER_UNIQUE_ID, but you should not have used that anyway.
不建议使用CLUSTER_WORKER_ID 这个环境变量

workers now kill themselves when they accidentally lose their connection to the master.
子进程会自杀,当他们偶然失去去与主进程的通信

3、http

http.Server emits ‘connect’ event instead of ‘upgrade’ when the CONNECT method is requested.

当连接被请求时,connect 事件会代替 upgrade 事件

http.ServerResponse sends Date: header by default. You can disable it by setting response.sendDate to false.

http的响应头会自动加上Date这个值,想要关掉它可以将 response.sendDate 设置为false

http.ClientRequest emits ‘connect’ event instead of ‘request’ when the CONNECT method is responded.

当node.js作为客户端去请求其他http服务器时,当对方响应,将会触发connect事件来代替request事件

4、net

net.listen(socketPath) no longer unlinks the socket unless you call server.close(). Automatic unlinking was subject to race conditions in multi-process setups. Also applies to http.listen(socketPath).

net.listen(socketPath)方法不会去断开socket,直到server.close()方法调用才会断开。自动断开的情况是多个进程竞争条件,同样适用于http.listen(socketPath).方法。

5、child_process

arguments and options arguments of child_process.fork() became an option.//修改了fork的api,详细见doc

the ‘exit’ event is now emitted right after the child process exits. It no longer waits for all stdio pipes to be closed.

//现在exit事件会等到进程退出后再出发,但是不会等待stdio pipes关闭

the ‘close’ event was added that has is emitted after the child has exited and all the stdio pipes are closed.

//同上,但是会等到stdio 管道关触发

6、url

url.parse() now parses IPv6 addresses. //支持IPv6的地址

url.parse() now escapes delimiters characters rather than truncating the input.//这句不是很理解

url.format() now escapes the auth section, and url.parse() decodes it.

//可以加入auth的片段支持

7、fs

path.exists() and path.existsSync() has moved to fs.exists() and fs.existsSync().

//以后查找文件是否存在改用fs.exists()方法了

fs.symlink type argument can now be ‘junction’ (for Windows only).

//junction是windows特有的,属于磁盘映射的工具,比如我们C盘容量过大了,于是我们把文件夹X(假设X有10个G)拷贝到D盘,然后使用junction工具将文件夹X映射回C盘,这样从C盘看访问路径还是C:/xxxx,也可以对其进行正常的修改删除,但是空间占用的是D盘的

fs.realpath and fs.lstat work the same on Windows as on Unix

//fs.realpath和fs.lstat在unix和windows上运行时一样了

8、console

console.timeEnd now throws when there’s no such label

没有 label 时输出当前时间戳

9、os

os.cpus().times is now supported on Windows

os.uptime now displays the correct uptime if the system’s current uptime is > 50 days on Windows

//这里主要是windows的支持问题