nodeschool.io 6
~~ MAKE IT MODULAR ~~
This problem is the same as the previous but introduces the concept of
modules. You will need to create two files to solve this.
Create a program that prints a list of files in a given directory,
filtered by the extension of the files. The first argument is the
directory name and the second argument is the extension filter. Print
the list of files to the console. You must use asynchronous I/O.
Your program must use a module to do most of the work. The module
must export a single function that takes three arguments: the
directory name, the filter string and a callback function.
The callback must return an error, and only an error, as the first
argument if one is passed from your call to `fs.readdir()`. If there
are no errors then the first argument to the callback must be null and
the second must be your filtered list of files in an array.
In the case of an error bubbling up to your original program file,
simply check for it and print an informative message to the console.
----------------------------------------------------------------------
HINTS:
Create a new module by creating a new file that just contains your
directory reading and filtering function. To define a single function
export you assign your function to the `module.exports` object,
overwriting what is already there:
module.exports = function (...) { ... }
Or you can use a named function and assign the name.
To use your new module in your original program file, use the
`require()` call in the same way that you `require('fs')` to load the
`fs` module. The only difference is that for local modules must be
prefixed with './'. So, if your file is named mymodule.js then:
var mymodule = require('./mymodule.js')
The '.js' is optional here and you will often see it omitted.
You now have the `module.exports` object in your module assigned to
the `mymodule` variable. Since you are exporting a single function,
`mymodule` is a function you can call!
Also keep in mind that it is idiomatic to check for errors and do
early-returns within callback functions:
foo(function (err, data) {
if (err)
return callback(err)
... // continue when no-error
})
----------------------------------------------------------------------
myModule.js
module.exports = function(dirName,regexStr,foo) {
var fs = require('fs');
var regex = new RegExp('\\.' + regexStr + '$')
fs.readdir(dirName, function (err, list) {
if(err) return foo(err)
list = list.filter(function (file) {
return regex.test(file);
})
foo(null,list);
})
}
myRequire.js
var mymodule = require("./mymodule.js"),
dirName = process.argv[2],
regexStr = process.argv[3];
mymodule(dirName,regexStr,function(err,list){
if(err) console.log(err);
list.forEach(function (file) {
console.log(file);
})
});
正如第5题一样,forEach 我平时基本不用,还有参数里面是function的,在执行的时候定义,我平时也不太容易想起来。
nodeschool.io 6的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 9
~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- C++实现有向权图的基本操作,界面友好,操作方便,运行流畅
Ⅰ.功能: .创建图 .展示全图 .添加顶点 .添加边 .删除顶点 .删除边 .查看指定边权值 .修改指定边权值 .输出两点间的所有简单路及路径对应权值 .销毁图 ps:关于9,如果不存在任何简单路, ...
- 【原创】OPA857 TEST模式使用
opa857 test mode: 配置opa857工作与test模式,设置tsst_sd为高电平连接到+Vs,test_in脚需要输入DC偏置以正确配置test模式.所需要增加的直流偏置大小因供电电 ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- eclipse svn异常:RA layer request failed 的解决方案
这几天svn总是出问题,网上搜了好多资料,今天才真正找到解决办法. RA layer request failedsvn: OPTIONS of 'https://192.168.0.104/svn/ ...
- Android 布局简要范例
Android的布局决定着实际的UI界面呈现情况,正是这些UI界面的组合与千变万化,才呈现出了各式各样的风格. 而这些基础的布局框架结构很重要,需要玩的很熟悉.我将以前参考的部分代码示例,所做的相关实 ...
- jquery的隐式类型转换
jquery的选择器想用变量来传,然后就纠结怎么写引号的问题??? 当时脑子就犯轴了,这个我要是传变量怎么写引号啊,我要是在最外层在加一层引号就不对了,就没法识别变量了,不加反而对了 那就用conso ...
- 简单模拟Spring管理Bean对象
1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...
- sscanf的用法(转)
队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...
- mysql 大数据量求平均值
需求是:对于一个设备,求一天内每个小时的平均值,一个月内每天的平均值,更通用的需求是,从起始时间到结束时间,每隔一段时间,求一个平均值.目前的解决策略是:在存储过程中进行处理,从起始时间到结束时间,切 ...
- 集群--LVS的DR模型配置
1.查看内核是否有IPVS内核模块 grep -i 'ip_vs' /boot/config-2.6.32-431.el6.x86_64