随笔分类 - nodejs
摘要:1.BLOCKING THE EVENT LOOP Node and JavaScript runtimes in general are single-threaded event loops. On each loop, the runtimeprocesses the next event in queue by calling the associated callback function. When that eventis done, the event loop fetches and processes the next event; this pattern contin.
阅读全文
摘要:Let’s say you want a function that does some I/O — such as parsing a log fi le — that will periodicallybe executed. Let’s give that function the generic name my_async_function. You could start byusing setInterval like this:var interval = 1000;setInterval(function() { my_async_function(function() ...
阅读全文
摘要:1 var fs = require('fs'); 2 3 /*cross device link 4 fs.rename('c:\\err.LOG','d:\\err.LOG',function(err){ 5 console.log(err.code); 6 }); 7 */ 8 9 move('c:\\err.LOG','d:\\err.LOG',function(err){10 if(err) throw err;11 });12 13 14 function move(oldpath,newpath,ca
阅读全文
摘要:一。目录➤ Understanding why you need buffers in Node➤ Creating a buffer from a string➤ Converting a buffer to a string➤ Manipulating the bytes in a buffer...
阅读全文
摘要:1.介绍一下事件驱动编程---快餐店点餐。 在基于线程的方式中(thread-based way)你到了柜台前,把你的点餐单给收银员或者给收银员直接点餐,然后等在那直到你要的食物准备好给你。收银员不能接待下一个人,除非你拿到食物离开。想接待更多的客户,容易!加更多的收银员!当然,我们知道快餐店其实不是这样工作的。他们其实就是基于事件驱动方式,这样收银员更高效。只要你把点餐单给收银员,某个人已经开始准备你的食物,而同时收银员在进行收款,当你付完钱,你就站在一边而收银员已经开始接待下一个客户。在一些餐馆,甚至会给你一个号码,如果你的食物准备好了,就呼叫你的号码让你去柜台取。关键的一点是,你没有阻.
阅读全文
摘要:1.child_process是Node.js的一个十分重要的模块,通过它可以实现创建多进程,以利用多核计算资源。child_process模块提供了四个创建子进程的函数,分别是spawn,exec,execFile和fork。其中spawn是最原始的创建子进程的函数,其他三个都是对spawn不同程度的封装。spawn只能运行指定的程序,参数需要在列表中给出,相当于execvp系统函数,而exec可以直接运行复杂的命令。child_process.spawn(command, [args], [options])child_process.exec(command, [options], ca
阅读全文
摘要:1.process是一个全局进程,你可以直接通过process变量直接访问它。 process实现了EventEmitter接口,exit方法会在当进程退出的时候执行。因为进程退出之后将不再执行事件循环,所有只有那些没有回调函数的代码才会被执行。在下面例子中,setTimeout里面的语句是没有办法执行到的。1 process.on('exit', function () {2 setTimeout(function () {3 console.log('This will not run');4 }, 100);5 console.log('exit&
阅读全文
摘要:1.今晚在在node.js的实验,在用socket.io.js时,发现html中有就改为了并把socket.io.js文件copy到目录下,启动服务后,console中出现unhandled socket.io url。一查,原来不用自己搞,服务端会自动生成的这个js文件。2.在浏览器中自动刷新显示当前server的时间 1 var app = require('http').createServer(handler); 2 var io = require('socket.io').listen(app); 3 var fs = require('fs
阅读全文