Node.JS之妙用初探 https://github.com/rvagg/learnyounode

介绍一个Node.JS界的大神:Rod Vagg,和他的nodeschool.io, 以及learyournode这么Node Module. https://github.com/rvagg/learnyounode. 

开发技术教程的人应该好好学习下。

来看其中一篇Node.JS的妙用。

写一个Node.JS的Module,调用这个Module,列出目录内容,以给定字串过滤,类似于 ls -l DIR/*STR*这个命令的输出。比如以下的运行结果:

oliverluan@localhost:~/Documents/Opt/exlearnyounode$ ls  ~/*scpt*
/Users/oliverluan/Counter.scpt   /Users/oliverluan/NumberLib.scpt /Users/oliverluan/test.scpt
/Users/oliverluan/John.scpt      /Users/oliverluan/magic.scpt     /Users/oliverluan/test2.scpt
oliverluan@localhost:~/Documents/Opt/exlearnyounode$ node testmodule.js ~/ scpt
Counter.scpt
John.scpt
NumberLib.scpt
magic.scpt
test.scpt
test2.scpt

Node.JS的代码如下:

testmodule.js

var f = require('./filtermodule.js');
var r = f(process.argv[2], process.argv[3], function (data) {
    //console.log(data);
});


filtermodule.js

odule.exports = function (dir, extname, callback) {
    var fs = require('fs');
    var v = '\\.' + extname + '$';
    //console.log(v);
    var re = new RegExp(v);
    //console.log(re);
    fs.readdir(dir, function(err, list) {
        if (err)
            return callback(err);

        var a = [];
        for (var i = 0 ; i < list.length; i++) {
            //console.log(list[i].match(re));
            if (list[i].match(re) != null) {
                console.log(list[i]);
                a.push(list[i]);
            }
        }
        return callback(null, a);
    });
};


Learyournode 安装和简介。


Install Node.js
Run sudo npm install learnyounode -g
Run learnyounode
.. profit!
learnyounode will run through a series of Node.js workshops. Starting at a basic "HELLO WORLD" and moving on to more advanced exercises about dealing with synchronous & asynchronous I/O, filesystem operations, TCP and HTTP networking, events and streams.

Once you have finished learnyounode, graduate to stream-adventure for a set of exercises that dig in to Node's streams.
Contributors
learnyounode is proudly brought to you by the following hackers:

Rod Vagg GitHub/rvagg Twitter/@rvagg
Andrey Sidorov GitHub/sidorares Twitter/@sidorares
Julián Duque GitHub/julianduque Twitter/@julian_duque
Lars-Magnus Skog GitHub/ralphtheninja Twitter/@ralphtheninja
Tim Inman GitHub/thehack Twitter/@timinman
Dan Flettre GitHub/Flet Twitter/@flettre


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值