当前位置: 首页 > 工具软件 > node-github > 使用案例 >

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

车靖琪
2023-12-01

介绍一个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


 类似资料: